Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • 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

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,

RE: Random Number Generator

tcllib would be very useful for that:


package require math

::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



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:


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

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!


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:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close