Tags: ability, isthe, procedure, procedureuses, programming, rand, seed, tcl

Questions about the seed of rand( )?

On Programmer » TCL

3,783 words with 1 Comments; publish: Wed, 30 Apr 2008 10:30:00 GMT; (20078.13, « »)

I have one question about how to use the seed of rand(). What I want is

the ability to make sure that my procedure (so far just one procedure

uses rand) will always go through the same set of random numbers

regardless of how many times rand( )have been called outside this

procedure. I thought about the following solutions:

Solution 1: At the top of the procedure, the random seed is set and at

the bottom of the procedure, the procedure saves the random seed which

will used for next call. However, neither rand() nor srand() return the

seed. I don't know if there is a way to return the seed.

Solution 2: Set the the seed using srand($seed) everytime at the top of

the procedure. Increment seed by 1. I tried this out but the numbers

are not so random.

(bin) 255 % set x ""

(bin) 256 % for {set n 0} {$n < 20} {incr n 1} {expr srand($n); lappend

x [expr rand()]}; join $x \n

0.0134695745136 <-- Beginning of increasing pattern.

0.131537788143

0.263075576286

0.394613364429

0.526151152573

0.657688940716

0.789226728859

0.920764517002 <-- End of increasing pattern.

0.0523023051453 <-- Beginning of increasing pattern. Repeats.

0.183840093288

0.315377881432

0.446915669575

0.578453457718

0.709991245861

0.841529034004

0.973066822147 <-- End of increasing pattern. Repeats.

0.104604610291

0.236142398434

0.367680186577

0.49921797472

I guess this may be because the random number generator (inside rand)

is linear. Actually I don't know much about random number generator but

I guess I am not using it the way it is designed for.

Is there a solution?

Thanks.

All Comments

Leave a comment...

  • 1 Comments
    • cykhung wrote:

      > I have one question about how to use the seed of rand(). What I want is

      > the ability to make sure that my procedure (so far just one procedure

      > uses rand) will always go through the same set of random numbers

      > regardless of how many times rand( )have been called outside this

      > procedure. I thought about the following solutions:

      > Solution 1: At the top of the procedure, the random seed is set and at

      > the bottom of the procedure, the procedure saves the random seed which

      > will used for next call. However, neither rand() nor srand() return the

      > seed. I don't know if there is a way to return the seed.

      > Solution 2: Set the the seed using srand($seed) everytime at the top of

      > the procedure. Increment seed by 1. I tried this out but the numbers

      > are not so random.

      >

      How about solution 3:

      Build your own separate random number generator and use it, so you know

      who uses it...

      One example would be:

      http://wiki.tcl.tk/random

      If you do not need lots of performance, you can always code a simple

      PRNG yourself...

      (for example the one used in Tcls expr...)

      proc my_rand {seedvar} {

      upvar 1 $seedvar seed

      set RAND_IA 16807

      set RAND_IM 2147483647

      set RAND_IQ 127773

      set RAND_IR 2836

      set RAND_MASK 123459876

      set seed [expr {$seed & 0x7fffffff}]

      if {($seed == 0) || ($seed == 0x7fffffff)} {

      set seed [expr {$seed ^ 123459876}]

      }

      set tmp [expr {$seed /${RAND_IQ}}]

      set newSeed [expr {${RAND_IA}*($seed - $tmp*${RAND_IQ}) -

      ${RAND_IR}*$tmp}]

      if {$newSeed < 0} {

      set newSeed [expr {$newSeed + ${RAND_IM}}]

      }

      set seed $newSeed

      set dResult [expr { $newSeed * (1.0/${RAND_IM})}]

      return $dResult

      }

      Usage:

      set seed 2

      puts [my_rand seed]

      #1; Wed, 30 Apr 2008 10:32:00 GMT