LCG随机数生成算法

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(int argc, char** argv)
{
    int n = 300000000;
    double U;
    int random_num;

    srand(time(NULL));  //初始化随机数种子
    for (int i = 0; i < 10; i++)
    {
        U = (double)rand() / RAND_MAX; //生成[0,1]之间的随机数
        random_num = (int)((n - 1)*U + 0.5); //生成[0,n-1]之间的随机数
        printf("%d ", random_num);
    }

    return 0;
}

猜你喜欢

转载自www.cnblogs.com/lovebay/p/12369983.html