I have forgotten
my Password

Or login with:

  • Facebookhttp://facebook.com/
  • Googlehttps://www.google.com/accounts/o8/id
  • Yahoohttps://me.yahoo.com
COST (GBP)
this unit 7.49
sub units 1.98
+
0

randomfast

Generates random numbers for a discrete distribution in constant time.
Controller: CodeCogs

add cart

Dependents

Info

Interface

C++
Excel

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 that has a distribution function and population mean

Example

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.
#include <iostream.h>
#include <conio.h>
#include <time.h>

#include <codecogs/stats/dists/discrete/discrete/randomfast.h>

void main() {

double P[4] = { 0.1, 0.1, 0.2, 0.6 }; double V[4] = { 3.14, 2.71, 0.33, 12.57 };

Stats::Dist::Discrete::Discrete::RandomFast A(4, P, V, false, 0.762); Stats::Dist::Discrete::Discrete::RandomFast B(4, P, false, time(0) / MERSENNEDIV);

for (int i = 0; i < 10; ++i) cout << A.genReal() << " "; cout << endl; getch();

cout << endl;

for (int i = 0; i < 10; ++i) cout << B.genReal() << " "; cout << endl getch();

}
Below you will find 10 numbers corresponding to the output of the first generator :
12.57 0.33 2.71 0.33 12.57 3.14 12.57 12.57 0.33 12.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.

References

  • MathWorld, http://mathworld.wolfram.com/DiscreteDistribution.html
  • The Newran03 random number generator library of Robert Davies, http://www.robertnz.net/nr03doc.htm

Authors

Will Bateman
Source Code

Source code is available when you buy a Commercial licence.

Not a member, then Register with CodeCogs. Already a Member, then Login.

Members of RandomFast

RandomFast

 
RandomFastintn
T*p
boolnormalise = true
doubles = 0.7323 )[constructor]
Constructor that sets up the class variables and initializes the associated random number generator with the given seed.
nThe number of values
pThe probability array of length n (should sum up to 1)
normaliseNormalises the probabilities defined in p (Default=true)
sDefault value = 0.7323

RandomFast

 
RandomFastintn
T*prob
T*val
doubles = 0.59012
boolmakecopy = false )[constructor]
Constructor that sets up the class variables and initializes the associated random number generator with the given seed.
nThe number of values
probThe probability array of length n
valThe set of values the generator may return
sDefault value = 0.59012
makecopyDefault value = false


RandomFast::init

RandomFast::init

 
template<class T> voidRandomFast<T>::initintn
T*p
boolnormalise )
normaliseDefault Value = true

SampleFast

 
intsampleFastintN
double*p
boolnormalise = true
doubleseed = 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.
#include <time.h>
...
sample(N,  p, time(0) / MERSENNEDIV);
If you require more advance behaviour, we strongly recommend that directly use the underlying class randomsample that is provided with this module.

Parameters

Nthe number of rates in custom distribution
pan array of N rates that define the occurrence rate of each event.
normaliseDefault Value = true
seedsets 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.

Not a member, then Register with CodeCogs. Already a Member, then Login.