这个东西,对于数学上的分析我看明白了90%.但并未做深入研究.这个代码是抄来的,核心部分是抄来的.感觉不错.
生成固定的随机数序列,只要种子一样的话.这个随机数生成器比较透明,而且较容易改动.很不错.
每次实现代码的时候,都会发现很多问题.而且,一旦写上了代码,时光就飞逝了...很不错.
好吧,帖出来吧.又是来网吧,又是边下电影边写Blog,呵呵.很不错.
/* 10-35-11-02-08-20.11.c -- 第十章第三十五题 */ #include <stdio.h> #define FALSE (0) #define TRUE (1) #define A (48271L) #define M (2147486647L) #define Q (M / A) #define R (M % A) unsigned long int Seed ; int main (void) ; int eatEnter (void) ; void initialize (unsigned long int initial_value) ; double random (void) ; int main (void) { unsigned long int initial_value ; char ch ; printf ("Please input a initial value (>= 0, enter 'q' to quit.):") ; while (scanf ("%lu", &initial_value) != 0) { printf ("Initial value has been initialized is : %lu/n", initial_value) ; eatEnter () ; initialize (initial_value) ; puts ("Enter 'q' to input a new initial value and enter any key else to generate a random value again.") ; while ((ch = getchar ()) != 'q') { if (TRUE == eatEnter ()) break ; printf ("It is %lf./n", random ()) ; } printf ("Please input a initial value (>= 0, enter 'q' to quit.):") ; } return 0 ; } int eatEnter (void) { if ('q' == getchar ()) return TRUE ; else return FALSE ; } void initialize (unsigned long int initial_value) { Seed = initial_value ; } double random (void) { unsigned long int temp_seed ; temp_seed = A * (Seed % Q) - R * (Seed / Q) ; if (temp_seed >= 0) Seed = temp_seed ; else Seed = temp_seed + M ; return (double) Seed / M ; }