| 我比較喜歡 下面的產生亂數的演算法 是按照 Apple CarbonLab 的方法實作的
 
 範圍是
 1 - 2147483646
 
 產生十億個亂數,需要時間比較久,
 大概需要 54.658秒
 
 
 
 
 // for fast random number generator
 
 #include "inc-01.h"
 // ----------------------------------------------
 
 void rnd1(int *s1)
 {
 // double x1, x2, x3;
 
 /* 130.078 seconds for 21E8
 x1= (double) (*s1);
 x2= x1*16807.0;
 x3= fmod(x2, 2147483647.0);
 (*s1)= (int) (x3 + 0.5);
 */
 
 // 116.423 seconds
 (*s1)= (int) ((fmod((((double) (*s1))*16807.0), 2147483647.0)) + 0.5);
 }
 // ----------------------------------------------
 
 int main(int argc, char *argv[])
 {
 // for rnd1(), rnd2(), irnd(), ...
 int t1, t2, s1, s2, no, min, max, i;
 double ct= 0.0, dt;
 
 no= 10;
 while (no > 0) {
 delay_ms((int) (1.1*1000));
 
 time1(&t1);
 s1= t1;
 
 rnd1(&s1);
 min= max= s1;
 
 time1(&t1);
 for (i=1;i<=no;i++) {
 rnd1(&s1);
 if (min > s1) min= s1;
 if (max < s1) max= s1;
 }
 time2(t1, &dt);
 
 printf(" no= %12ld, min= %12ld, max= %12ld, dt= %10.3lf \n", no, min, max, dt);
 no*= 10;
 }
 
 pause();
 return EXIT_SUCCESS;
 }// end of main()
 
 
 |