×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
• Talk With Other Members
• Be Notified Of Responses
• Keyword Search
Favorite Forums
• Automated Signatures
• Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

#### Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

# 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.

Raj.

### RE: Random Number Generator

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.

### RE: Random Number Generator

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

### RE: Random Number Generator

Or even better:

#### CODE

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

### RE: Random Number Generator

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)

#### Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

#### Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Close Box

# Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

• Talk To Other Members
• Notification Of Responses To Questions
• Favorite Forums One Click Access
• Keyword Search Of All Posts, And More...

Register now while it's still free!