线性同余随机数算法简单地实现

#include
#include
#include"MyOutput.hpp"
using namespace std;

static unsigned int a = 17;
static unsigned int b = 139;
static const unsigned long BASE_VALUE = ULONG_MAX;/*unsigned long max value*/
static unsigned int r = static_cast(time(0)/*time_t is long*/);

void setRandSeed(unsigned int v){
    r = v;
}

const double random(){
    r = r*a + b;
    // println(r);
    r = r%BASE_VALUE;
    // println(r);
    double ret  = static_cast(r);
    // println(ret);
    ret = ret / BASE_VALUE;
    // println(ret);
    
    return ret;
}

/**
 * For Test Random
 * P
 * 
 * 
 */ 
template
const double varValiance(T arr[],int length){
    double X = 0;
    for(int i=0;i


8
type name: __int64
type raw name: ._J
0.0191321 0.325246 0.52919 0.996228 0.935875 0.909874 0.467865 0.953699 0.212891 0.619144 0.525455 0.93273 0.856417 0.559086 0.504462
0.575849 0.789438 0.420445 0.147573 0.508739 0.648562 0.0255609 0.434536 0.387112 0.580896 0.875237 0.879034 0.943583 0.0409189 0.695621 0.825562 0.0345504 0.587357 0.985076 0.746298 0.687062 0.680062 0.561058 0.537982 0.145699 0.476882 0.106988 0.818791 0.919443 0.630537 0.71913 0.225209 0.828559 0.085498 0.453465 0.70891 0.0514622 0.874857 0.872566 0.833624 0.1716 0.917204 0.592472 0.072019 0.224324 0.813504 0.829563 0.102574 0.743763 0.643967 0.947438 0.106453 0.809702 0.764929 0.00378704 0.0643797 0.0944542 0.605721 0.297266 0.053514 0.909738 0.465551 0.914372 0.544327 0.253551 0.310372 0.276322 0.697473 0.857045 0.56976 0.685922 0.660671 0.231399 0.933791 0.874445 0.865571 0.714701 0.149912 0.548508 0.324636 0.518813 0.81982 0.936942 0.928015 0.776255
0.196334
======================

理想情况下,10个元素,赋值总次数100,平均每个元素的赋值次数: 10
方差为: 13.3333
相差: 3.33333
=======================

理想情况下,100个元素,赋值总次数1000,平均每个元素的赋值次数: 10
方差为: 9.71717
相差: 0.282828
=======================

理想情况下,10个元素,赋值总次数1021,平均每个元素的赋值次数: 102.1
方差为: 227.211
相差: 125.111
=======================

理想情况下,100个元素,赋值总次数19999,平均每个元素的赋值次数: 199.99
方差为: 212.374
相差: 12.3836
=======================

理想情况下,100个元素,赋值总次数888,平均每个元素的赋值次数: 8.88
方差为: 9.37939
相差: 0.499394
=======================

理想情况下,1000个元素,赋值总次数888,平均每个元素的赋值次数: 0.888
方差为: 0.868324
相差: 0.0196757
=======================

理想情况下,10000个元素,赋值总次数77777,平均每个元素的赋值次数: 7.7777
方差为: 7.91327
相差: 0.135574
========================

理想情况下,1000个元素,赋值总次数10000000,平均每个元素的赋值次数: 10000
方差为: 9302.13
相差: 697.872





你可能感兴趣的:(Math,C/C++,Algorithm)