Generates a random deviate from the discrete distribution.
int
m_n
T*
m_val
int
sample (int N, double* p, bool normalise=true, double seed=0.1234)
Returns the next random deviate from a distribution defined by the rates p.
These functions are available for Microsoft Excel (Login to download):
Integer
cc_discreteSample (Integer N, Range p, Boolean normalise, Real seed)
Use the following HTML code to embed the calculators within other websites:
Class RandomSample
This class generates random numbers from a discrete distribution and is optimised for sampling a small population many times.
The discrete statistical distribution can only take discrete values. It has probability function
When this class is first initialised, it calculates the CDF of the probability array (so there is a small initialisation cost). With each request for a new random number, it generates an uniform random deviate
between 0 and 1 and finds its corresponding abscissa value, as illustrated in the graph below.
A binary search is used to detect the interval in which that value is situated,
which in return gives the actual discrete random number. For instance if the ordinate
would be approximately equal to as in the graph, the corresponding abscissa
would be around , which corresponds to an index in the values array that
finally gives the random number.
Speed
The average running time for generating 100,000,000 random numbers using this class
on a 750MHz microprocessor is 16 seconds.
Example 1
The given example displays 20 random floating point numbers from a discrete distribution.
It uses two different generators to achieve this. The first generator uses a particular value
to initialize the seed, while the second one is using the system timer. Notice that it was necessary
to divide the timer by the <em> MERSENNEDIV </em> value in order to keep the seed in the interval from 0 to 1.
Since the seed of the first generator is never changed, the first 10 numbers will always
remain the same. However since the second generator is initialized via the system timer,
the next 10 numbers will obviously vary with each execution of the program.
Also notice the declaration of the probability array P and value array V. They are used as
constructor arguments when declaring the generators to specify the probabilities P[i] associated
to each value V[i] in the values table. Always make sure that the components of P sum up to 1.
Constructor that sets up the class variables and initializes the associated random number generator with the given seed.
V[n] = {0, 1, 2, ..., n - 1}
n
Number of points.
p
Array of probabilities (should sum up to 1; otherwise you must normalise the data by setting normalise=true).
val
Array of associated values. It not specified they will be a sequential series from 0 to n-1. i.e.
normalise
Set this value to 'true' to normalise the data provided in /e p, so it sums up to 1.
seed
Default value = 0.2354
RandomSample
RandomSample(
int
n
T*
p
bool
normalise = true
double
seed = 0.2354
)[constructor]
Constructor that sets up the class variables and initializes the associated random number generator with the given seed.
n
Number of points.
p
Array of probabilities (should sum up to 1; otherwise you must normalise the data by setting normalise=true).
normalise
Set this value to 'true' to normalise the data provided in /e p, so it sums up to 1.
seed
Default value = 0.2354
Init
voidinit(
int
n
T*
p
bool
normalise = true
)
normalise
Default Value = true
Sample
intsample(
int
N
double*
p
bool
normalise = true
double
seed = 0.1234
)
This function is a simple wrapper around the randomsample class provided in this module. It uses a static to keep a single instance of this class, so that each call to this function returns a new random number. As a result this function is not necessarily thread safe, in the sense that with an identical initial seed, the sequence of random numbers may differ on a multitasking or multi threaded system.
The seed is only set on the first call to this function. Thereafter this parameter is ignored. If you do no want to set the seed, then we suggest you use the system clock the first time you call this function, i.e.
If you require more advance behaviour, we strongly recommend that you directly use the underlying class randomsample that is provided with this module.
Parameters
N
the number of rates in custom distribution
p
an array of N rates that define the occurrence rate of each event.
normalise
Default Value = true
seed
sets the initial seed for the random generator. Only used in the first call to this function
Returns
this function returns an index in the range 0 to (N-1), which act as an index into the array define in p.
Source Code
Source code is available when you buy a Commercial licence.