## Random Number Generator

## Random Number Generator

(OP)

hi all,

I used the Random Number Generator, page 85, from the book "Practical Programming in Tcl and Tk(3rd edition)" by Brent B. Welch. As it is mentioned there, it was adapted from a different source. anyway, the point is this RNG is having a flaw. It took me one whole day to recognize this as my program was giving some bizzare results. finally I found out that this RNG is generating repeated sequence of random numbers.

for example if I use this RNG to produce ten random numbers between 1 and 10, and if I call the same routine for 300 times, that means I get 300 number sequences of ten numbers per each sequence. these sequences are getting repeated after a while, like 100th sequence is same as 240th, 101st sequence is same as 241st and so on.

My request is can anyone tell me where I can get a better RNG to use in my Tk script.

thnx in adv,

Raj.

I used the Random Number Generator, page 85, from the book "Practical Programming in Tcl and Tk(3rd edition)" by Brent B. Welch. As it is mentioned there, it was adapted from a different source. anyway, the point is this RNG is having a flaw. It took me one whole day to recognize this as my program was giving some bizzare results. finally I found out that this RNG is generating repeated sequence of random numbers.

for example if I use this RNG to produce ten random numbers between 1 and 10, and if I call the same routine for 300 times, that means I get 300 number sequences of ten numbers per each sequence. these sequences are getting repeated after a while, like 100th sequence is same as 240th, 101st sequence is same as 241st and so on.

My request is can anyone tell me where I can get a better RNG to use in my Tk script.

thnx in adv,

Raj.

## RE: Random Number Generator

tcllib would be very useful for that:

## CODE

::math::random

::math::random ?value1? ?value2?

Return a random number. If no arguments are given, the number is a floating point value between 0 and 1. If one argument is given, the number is an integer value between 0 and value1. If two arguments are given, the number is an integer value between value1 and value2.

## RE: Random Number Generator

## CODE

::math::statistics::random-normal mean stdev number

Return a list of "number" random values satisfying a normal distribution with given mean and standard deviation.

mean - Mean value of the distribution

stdev - Standard deviation of the distribution

number - Number of values to be returned

::math::statistics::random-exponential mean number

Return a list of "number" random values satisfying an exponential distribution with given mean.

mean - Mean value of the distribution

number - Number of values to be returned

::math::statistics::random-uniform xmin xmax value

Return a list of "number" random values satisfying a uniform distribution with given extremes.

xmin - Minimum value of the distribution

xmin - Maximum value of the distribution

number - Number of values to be returned

## RE: Random Number Generator

## CODE

set rnum [expr {int(1 + rand() * $poss)}]

return $rnum

}

## RE: Random Number Generator

static const long M = 2147483647L; //2^31 -1

static const long Q = M / A;

static const long R = M % A;

unsigned long

Random::RandomLong( )

{

long TmpSeed = A * ( Seed % Q ) - R * ( Seed / Q );

if( TmpSeed >= 0 )

Seed = TmpSeed;

else

Seed = TmpSeed + M;

return Seed;

}

Xi+1 = A(Xi(mod Q))-R[Xi/Q]+Mdel(Xi)

All of this is taken from:

Algorithms, Data Structures, and Problem Solving with C++, by Mark Allen Weiss

He also discribes rand functions for different distributions you may or may not be looking for. (gaussian as above, poisson, etc)