sampleFast (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_sampleFast (Integer N, Range p, Boolean normalise, Real seed)
Use the following HTML code to embed the calculators within other websites:
Class RandomFast
This random generator is an alternative of Discrete/RandomSample, and is optimised for discrete sampling of large distribution when you require constant time random number generation. However, due to rounding errors with this approach, if your distribution is very large, then you should use Discrete/RandomSample for more accuracy.
This statistical distribution can take only discrete values, with a probability function
The following 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 with the MERSENNEDIV value in order to keep the seed in the (0, 1) interval.
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.
Below you will find 10 numbers corresponding to the output of the first generator :
12.570.332.710.3312.573.1412.5712.570.3312.57
Speed
The average running time for generating 100,000,000 random numbers using this class
on a 750MHz microprocessor is 8 seconds.
Warning
Due to rounding errors, this class is not suited for use with very large number of input probability values,
i.e. when n exceeds 10,000 values, the accuracy of the random numbers deteriorates.
Constructor that sets up the class variables and initializes the associated
random number generator with the given seed.
n
The number of values
p
The probability array of length n (should sum up to 1)
normalise
Normalises the probabilities defined in p (Default=true)
s
Default value = 0.7323
RandomFast
RandomFast(
int
n
T*
prob
T*
val
double
s = 0.59012
bool
makecopy = false
)[constructor]
Constructor that sets up the class variables and initializes the associated
random number generator with the given seed.
n
The number of values
prob
The probability array of length n
val
The set of values the generator may return
s
Default value = 0.59012
makecopy
Default value = false
RandomFast::init
RandomFast::init
template<class T> voidRandomFast<T>::init(
int
n
T*
p
bool
normalise
)
normalise
Default Value = true
SampleFast
intsampleFast(
int
N
double*
p
bool
normalise = true
double
seed = 0.1234
)
This function is a simple wrapper around the randomfast 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 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.