亂數產生器
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萬個亂數。
/*for rnd1(), rnd2(), irnd(), init_rnd()*/// ----------------------------------------------#include "inc01.h"// ----------------------------------------------void main(){// r(n)= (r(n-1)*a + b) mod cdouble 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 s1x1= (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()
页:
[1]