randomWH API
Generation of Pseudo-Random Variates
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
generator Struct Reference

Typedef of struct to store and process the four generator seeds. More...

#include <randomWH.h>

Data Fields

Variables
long int x
 
long int y
 
long int z
 
long int t
 
enum BOOLEAN initialised
 
Member Functions
void(* init )(struct generator *self, long int x, long int y, long int z, long int t)
 Initialise the generator with four values of type long int. More...
 
double(* randomWH )(struct generator *self)
 Generate a (pseudo-)random number drawn from the U(0,1) distribution. More...
 
long int *(* read )(struct generator *self)
 Read the current state of the pseudo-random number generator. More...
 
enum BOOLEAN(* bernoulli )(struct generator *self, double prob)
 Generate a binomially distributed random variate. More...
 
long int(* geometric )(struct generator *self, double prob)
 Generate a geometrically distributed random variate. More...
 
long int(* binomial )(struct generator *self, long int trials, double prob)
 Generate a binomially distributed random variate. More...
 
long int(* fairdie )(struct generator *self, long int lower_inclusive, long int upper_inclusive)
 Generate a discrete-valued uniformly distributed random variate drawn from a specified range. More...
 

Detailed Description

Typedef of struct to store and process the four generator seeds.

In ordinary usage, you will not use this typedef directly. Instead, you will use the instance of it called Congruence which is pre-initialised in randomWH.c. An exception would be if your code requires multiple instances of the random number generator. A second instance can be generated using the following code (failure to initialise this correctly will almost always cause fatal errors at run time).

#include "randomWH.h"
Congruence generator2 = {
.x=0,
.y=0,
.z=0,
.t=0,
.initialised=isfalse,
.randomWH=randomWH_null,
.bernoulli=randomWH_bernoulli,
.geometric=randomWH_geometric,
.binomial=randomWH_binomial,
.fairdie=randomWH_fairdie,
};

This generator can be accessed using generator2.init(), generator2.read(), etc.

If multiple generators are instantiated, the number sequences generated may overlap; this is certain to occur if two or more are seeded with the same values.

See Also
congruent

Definition at line 147 of file randomWH.h.

Field Documentation

enum BOOLEAN(* bernoulli)(struct generator *self, double prob)

Generate a binomially distributed random variate.

Usage

#include <stdio.h>
#include "randomWH.h"
Congruence generatorX={
.x=0,
.y=0,
.z=0,
.t=0,
.initialised=isfalse,
.randomWH=randomWH_null,
.bernoulli=randomWH_bernoulli,
.geometric=randomWH_geometric,
.binomial=randomWH_binomial,
.fairdie=randomWH_fairdie,
};
.
.
.
if (generatorX.bernoulli(&generatorX,0.5)==istrue)
printf("Heads!\n");
else
printf("Tails!\n");
Parameters
selfA pointer to the calling struct (required since C does not support the this keyword).
probThe probability of success
Returns
a BOOLEAN variable indicating an outcome of success or failure

Definition at line 324 of file randomWH.h.

long int(* binomial)(struct generator *self, long int trials, double prob)

Generate a binomially distributed random variate.

Usage

#include <stdio.h>
#include "randomWH.h"
Congruence generatorX={
.x=0,
.y=0,
.z=0,
.t=0,
.initialised=isfalse,
.randomWH=randomWH_null,
.bernoulli=randomWH_bernoulli,
.geometric=randomWH_geometric,
.binomial=randomWH_binomial,
.fairdie=randomWH_fairdie,
};
long int wheelspin;
.
.
.
wheelspin = generatorX.binomial(&generatorX,100,1.0/38.0)
printf("My (un)lucky number came up %ld times in 100 spins of a roulette wheel\n", wheelspin);
Parameters
selfA pointer to the calling struct (required since C does not support the this keyword).
trialsThe number of trials
probThe probability of success in each trial

Definition at line 395 of file randomWH.h.

long int(* fairdie)(struct generator *self, long int lower_inclusive, long int upper_inclusive)

Generate a discrete-valued uniformly distributed random variate drawn from a specified range.

Usage

#include <stdio.h>
#include "randomWH.h"
Congruence generatorX={
.x=0,
.y=0,
.z=0,
.t=0,
.initialised=isfalse,
.randomWH=randomWH_null,
.bernoulli=randomWH_bernoulli,
.geometric=randomWH_geometric,
.binomial=randomWH_binomial,
.fairdie=randomWH_fairdie,
};
long int lotto;
.
.
.
lotto = generatorX.fairdie(&generatorX,1L,45L)
printf("The first Lotto number drawn was %ld\n", lotto);
Parameters
selfA pointer to the calling struct (required since C does not support the this keyword).
lower_inclusivethe lowest value of variate that can be returned
upper_inclusivethe highest value of variate that can be returned

Definition at line 432 of file randomWH.h.

long int(* geometric)(struct generator *self, double prob)

Generate a geometrically distributed random variate.

Usage

#include <stdio.h>
#include "randomWH.h"
Congruence generatorX={
.x=0,
.y=0,
.z=0,
.t=0,
.initialised=isfalse,
.randomWH=randomWH_null,
.bernoulli=randomWH_bernoulli,
.geometric=randomWH_geometric,
.binomial=randomWH_binomial,
.fairdie=randomWH_fairdie,
};
long int patience;
.
.
.
patience = generatorX.geometric(&generatorX,1.0/52.0)
printf("It took %ld draws before my card appeared\n", patience);
Parameters
selfA pointer to the calling struct (required since C does not support the this keyword).
probThe probability of success in each trial

Definition at line 360 of file randomWH.h.

void(* init)(struct generator *self, long int x, long int y, long int z, long int t)

Initialise the generator with four values of type long int.

We need to populate the congruent structure with seed values before calling randomWH(). This function isolates the initialisation from the implementation.

Usage

#include "randomWH.h"
Congruence generatorX={
.x=0,
.y=0,
.z=0,
.t=0,
.initialised=isfalse,
.randomWH=randomWH_null,
.bernoulli=randomWH_bernoulli,
.geometric=randomWH_geometric,
.binomial=randomWH_binomial,
.fairdie=randomWH_fairdie,
};
.
.
.
generatorX.init(&generatorX, 1L,2L,3L,4L);
Parameters
xThe seed for the first generator
yThe seed for the second generator
zThe seed for the third generator
tThe seed for the fourth generator
selfA pointer to the calling struct (required since C does not support the this keyword).
Note
Initialising the generator with the same seeds from program run to program run will produce exactly the same sequence . Usually in simulation programs (particularly when testing and debugging them), this is exactly what you want.

Definition at line 198 of file randomWH.h.

enum BOOLEAN initialised

initialised to false; set to true when function pointers updated

Definition at line 155 of file randomWH.h.

double(* randomWH)(struct generator *self)

Generate a (pseudo-)random number drawn from the U(0,1) distribution.

Usage

#include <stdio.h>
#include "randomWH.h"
#define SIM_DURATION 40000000L
Congruence generatorX={
.x=0,
.y=0,
.z=0,
.t=0,
.initialised=isfalse,
.randomWH=randomWH_null,
.bernoulli=randomWH_bernoulli,
.geometric=randomWH_geometric,
.binomial=randomWH_binomial,
.fairdie=randomWH_fairdie,
};
.
.
.
long int count;
double mean=0, moment2=0, unif, variance;
generatorX.init(&generatorX,1L,2L,3L,4L);
for(count =0;count<SIM_DURATION;count++){
unif = generatorX.randomWH(&generatorX);
mean += unif;
moment2 += (unif*unif);
}
mean /= SIM_DURATION;
moment2 /= (SIM_DURATION-1.0); // using Bessel's correction
variance = moment2-mean*mean*(SIM_DURATION)/(SIM_DURATION-1.0); // Bessel's correction continued
printf("Mean after %ld iterations is %lf; sample variance is %lf\n", SIM_DURATION, mean, variance);
Returns
a double value drawn (to a good approximation) from a uniform distribution in the range [0,1]
See Also
Congruence_init()
Congruence_read()
http://www.sciencedirect.com/science/article/pii/S0167947306001836?np=y
Warning
Never use a random number generator that you have not tested extensively first. Never use a generator if you do not know and understand the algorithm it implements.
Note
Initialising the generator with the same seeds from program run to program run will produce exactly the same sequence . Usually in simulation programs (particularly when testing and debugging them), this is exactly what you want.

Definition at line 248 of file randomWH.h.

long int*(* read)(struct generator *self)

Read the current state of the pseudo-random number generator.

Usage

#include <stdio.h>
#include "randomWH.h"
Congruence generatorX={
.x=0,
.y=0,
.z=0,
.t=0,
.initialised=isfalse,
.randomWH=randomWH_null,
.bernoulli=randomWH_bernoulli,
.geometric=randomWH_geometric,
.binomial=randomWH_binomial,
.fairdie=randomWH_fairdie,
};
.
.
.
long int* read_con;
int count;
read_con=generatorX.read(&generatorX);
for(count=0;count<4;count++)
printf("%ld\n", read_con[count]);
Returns
A pointer to an array of four long int values .
See Also
init()
Note
This function can be used immediately prior to program shutdown to read the current seed values prior to saving them in persistent storage. Loading these values from persistent storage during program initialisation and using them as the arguments in a call to the relevant .init() will allow execution to resume exactly as if the previous program run had continued.
Warning
Only four values are returned. Do not use an index greater than 3 to access the array.

Definition at line 289 of file randomWH.h.

long int t

The seed for the fourth generator

Definition at line 154 of file randomWH.h.

long int x

The seed for the first generator

Definition at line 151 of file randomWH.h.

long int y

The seed for the second generator

Definition at line 152 of file randomWH.h.

long int z

The seed for the third generator

Definition at line 153 of file randomWH.h.


The documentation for this struct was generated from the following file: