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

#include<iostream>
#include<ctime>
#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<unsigned int>(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<double>(r);
    // println(ret);
    ret = ret / BASE_VALUE;
    // println(ret);
    
    return ret;
}

/**
 * For Test Random
 * P
 * 
 * 
 */ 
template<typename T>
const double varValiance(T arr[],int length){
    double X = 0;
    for(int i=0;i<length;i++){
        X += arr[i];
    }
    X /= length;
    double Sigma2 = 0;
    for(int i=0;i<length;i++){
        Sigma2 += (arr[i] - X)*(arr[i] - X);
    }
    Sigma2 /= length-1;
    return Sigma2;
}

void testP(int numOfArr,int testCount){

    double *count = new double[numOfArr]{0};
 
    // int count[100] = {0};
    
    for(int i=0;i<testCount;i++){
        count[int(numOfArr*random())] += 1; 
    }
    // println("输出每个元素的赋值次数:");
    // for(int i=0;i<numOfArr;i++){
    //     print(count[i],' ');
    // }
    println();


    double first = testCount;
    double second = numOfArr;

    double idealVal = first/second;
    
    println("理想情况下,",numOfArr,"个元素,","赋值总次数",testCount,",平均每个元素的赋值次数: ",idealVal);
    double varVal = varValiance(count,numOfArr);
    println("方差为: ",varVal);
    double abs = fabs(idealVal-varVal);
    println("相差: ",abs);

    delete []count;

}

int main(){

    println(sizeof time_t);
    const type_info & type = typeid(time_t);
    println("type name: ",type.name());
    println("type raw name: ",type.raw_name());

    for(int i=0;i<100;i++){
        print(random(),' ');
    }
    println();

    double d = random();
    println(d);

    println("======================");
    /** 
     * testP(随机数个数,所有元素赋值总次数)
     * 简单验证下随机生成函数的效果.
    */
    testP(10,100);
    
    println("=======================");
    testP(100,1000);
    println("=======================");
    testP(10,1021);
    println("=======================");
    testP(100,19999);
    println("=======================");
    testP(100,888);
    println("=======================");
    testP(1000,888);
    println("=======================");
    testP(10000,77777);
    println("========================");
    testP(1000,10000000);


    return 0;
  
}


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





已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页