Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Random Number Generator

Status
Not open for further replies.

RajVerma

Programmer
Jun 11, 2003
62
DE
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.
 
again...
tcllib would be very useful for that:

Code:
package require math 
::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.
 
or

Code:
package require math::statistics



::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
 
Or even better:
Code:
proc randnum{num} {
 set rnum [expr {int(1 + rand() * $poss)}]
 return $rnum
}
 
static const long A = 48271L;
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)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top