Simple programs to run pseudo-random number generators. the programs are separated by %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Attached you find a simple programs to test linear concruential generators: a) simple.c taken from the notes of J-S Wang b) drand48.c drand48() of the C-library c) g05.c g05caf() from NAG-library (my own implementation) Exercise: a) Have a look at the programs (print them) b) Compile the programs e.g. gcc -O g05.c -o g05.exe execute the programs. d) Modify the programs such that 10000000 random numbers are generated (don't print them on the screen) example: g05caf: time test.exe give a ... 12321 39.17u 0.10s 0:45.57 86.1% The first number gives the CPU-time in seconds. e) Check the uniform distribution in [0,1) by generating 10^8 numbers ; count the numbers in the intervals [0,0.1) , [0.1,0.2) ... %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% /* Testprogram with the random-number generator drand48 */ /* The generator is of the linear congruential type */ /* for more information : man drand48 */ #include double drand48(); /* function with the generator*/ /* program that calls the generator*/ void main(void) { int i; unsigned short int seed16v[3]; printf("give a Start value for the Generator \n"); scanf("%d",&seed16v[0]); seed48(seed16v); for(i=0;i<25;i++) printf("%15.13f \n",drand48()); } %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% /* Testprogram with the random-number generator G05CAF */ /* The generator is of the linear congruential type */ /* with a=13^13 , c=0 and m=2^59 */ /* globally defined variables for g05caf */ unsigned int ran2,ran3,ran4; void ginit(); /* function for the Initialisation of the generator*/ double g05caf(); /* function with the generator*/ /* program that call the generator*/ void main(void) { int i; ginit(); for(i=0;i<25;i++) printf("%15.13f \n",g05caf()); } void ginit() { int i0; printf(" give a positive integer to initialize the generator \n"); scanf("%d",&i0); ran2=0; ran3=(2*i0+1)>>15; ran4=(2*i0+1)&32767u; g05caf(); } double g05caf() { /* the random number generator G05CAF */ #define MASK15 32767u #define MASK29 536870911u #define INV2 0.186264514923096e-8 #define INV3 0.568434188608080e-13 #define INV4 0.173472347597681e-17 unsigned int ranu2,ranu3,ranu4; double xx; ranu4=17917u*ran4; ranu3=ranu4>>15; ranu3=ranu3+13895u*ran4+17917u*ran3;ranu2=ranu3>>15; ranu2=ranu2+282074u*ran4+653080135u*ran3+455329277u*ran2; ran2=ranu2&MASK29; ran3=ranu3&MASK15; ran4=ranu4&MASK15; xx=(INV2 * (double) ran2)+(INV3 * (double) ran3) +(INV4 * (double) ran4); return xx; } %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% /* program with a linear concruential generator */ /* with a=69069 , c=0 and m=2^32 */ /* for test purpose only!!! not recommended for serious use !!! */ /* globally defined Variable for the generator */ unsigned int ran; void init(); /* function for the initialization */ double ranx(); /* function with the generator*/ /* program that calls the generator */ void main(void) { int i; init(); for(i=0;i<25;i++) printf("%15.13f \n",ranx()); } void init() { int i0; printf(" give a positive integer for the initialization of the generator \n"); scanf("%d",&i0); ran=i0; } double ranx() { double xx; ran=ran*69069u; xx=0.2328306436539e-09*ran; return xx; }