亂數產生器

r(n)= (r(n-1)*a + b) mod c
第 n個亂數,等於 前一個亂數,乘以 a, 加上 b,
mod c

a= 7^5

b= 0

c= (2^31) - 1

產生 20億個亂數,需時約 350秒
平均每秒,大約可以產生 600萬個亂數。

[code]

/*
for rnd1(), rnd2(), irnd(), init_rnd()
*/
// ----------------------------------------------

#include "inc01.h"
// ----------------------------------------------

void main()
{
// r(n)= (r(n-1)*a + b) mod c
double a,b,c, x1,x2,x3, dt, ct1;
int s1, s2, t1;

a= pow(7, 5);
b= 0;
c= (pow(2, 31)) - 1;

skip(1);
printf("a= %.3lf, b= %.3lf, c= %.3lf\n", a, b, c);
// ----------------------------------------------

time1(&t1);
s1= t1;
s2= s1;
ct1= 0;

do {
// get a new s1
x1= (double) s1;
x2= x1*a + b;
x3= fmod(x2, c);

s1= (int) (x3 + 0.5);
ct1++;
} while (s1 != s2);
// ----------------------------------------------

time2(t1, &dt);
skip(1);
printf("ct1= %.0lf, dt= %10.4lf\n", ct1, dt);
}// end of main()


[/code]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值