randomWH API
Generation of Pseudo-Random Variates
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
randomWH_usage.c
Go to the documentation of this file.
1 
34 #include <stdio.h>
35 #include <stdlib.h>
36 
37 
38 #include "randomWH.h"
39 #define SIM_DURATION 40000000L
40 
41 /* Example of struct initialisation
42  * A struct called congruent is pre-initialised for you
43  */
44 
46  .x=0,
47  .y=0,
48  .z=0,
49  .t=0,
50  .initialised=isfalse,
51  .init=Congruence_init,
52  .randomWH=randomWH_null,
53  .read=Congruence_read,
54  .bernoulli=randomWH_bernoulli,
55  .geometric=randomWH_geometric,
56  .binomial=randomWH_binomial,
57  .fairdie=randomWH_fairdie,
58 };
59 
60 void Congruence_dump(Congruence* generatorZ);
61 
62 void Congruence_dump(Congruence* generatorZ){
63  long int* read_con;
64  int count;
65  char seednames[]={'x','y','z','t'};
66  read_con=generatorZ->read(generatorZ);
67  for(count=0;count<4;count++)
68  printf("seed %c = %ldL\n", seednames[count], read_con[count]);
69 }
70 
71 
72 int main(){
73  long int count;
74  double mean=0, moment2=0, unif, variance, prob;
75  Congruence* sequence = &congruent2;
76 /* Generate a sequence and calculate its first and second moment */
77  congruent.init(&congruent,1L,2L,3L,4L);
78  for(count =0;count<SIM_DURATION;count++){
79  unif = congruent.randomWH(&congruent);
80  mean += unif;
81  moment2 += (unif*unif);
82  }
83  mean /= SIM_DURATION;
84  moment2 /= (SIM_DURATION-1.0); /* using Bessel's correction */
85  variance = moment2-mean*mean*(SIM_DURATION)/(SIM_DURATION-1.0); /* Bessel's correction continued */
86  printf("Mean after %ld iterations is %lf; sample variance is %lf\n", SIM_DURATION, mean, variance);
87  printf("Seeds to use for next run:\n");
89 
90  printf("\n");
91 
92 /* Generate the same sequence, using a second instance of the generator */
93  mean = moment2 = 0;
94  sequence->init(sequence, 1L,2L,3L,4L);
95  for(count =0;count<SIM_DURATION;count++){
96  unif = sequence->randomWH(sequence);
97  mean += unif;
98  moment2 += (unif*unif);
99  }
100  mean /= SIM_DURATION;
101  moment2 /= (SIM_DURATION-1.0); /* using Bessel's correction */
102  variance = moment2-mean*mean*(SIM_DURATION)/(SIM_DURATION-1.0); /* Bessel's correction continued */
103  printf("Mean after %ld iterations is %lf; sample variance is %lf\n", SIM_DURATION, mean, variance);
104  printf("Seeds to use for next run:\n");
105  Congruence_dump(sequence);
106 
107 /* Estimate means of some distributions */
108  prob = 0.1;
109  mean = 0.0;
110  for(count = 1;count<=50000;count++)
111  mean += congruent.geometric(&congruent,prob);
112  printf("\nMean number of tries until first success, each trial with probability %lf: %lf\n"
113  ,prob,mean/(count-1));
114  prob=0.5;
115  mean = 0.0;
116  for(count = 1;count<=50000;count++)
117  mean += congruent.bernoulli(&congruent,prob)==istrue?1:-1;
118  printf("\n%ld fair coin tosses: Mean: %lf (should approach zero)\n",count-1,mean/(count-1));
119  prob = 0.666666666666666666;
120  mean = 0.0;
121  for(count = 1;count<=50000;count++)
122  mean += congruent.binomial(&congruent,30L,prob);
123  printf("\nMean number of successes in 30 trials with prob. of success %lf is %lf", prob, mean/(count-1));
124  printf("\n");
125 
126  mean = 0.0;
127  for(count = 1;count<=50000;count++)
128  mean += congruent.fairdie(&congruent,1L,6L);
129  printf("\nMean result of %ld dice throws: %lf", count-1, mean/(count-1));
130  printf("\n");
131 
132  return EXIT_SUCCESS;
133 }