random

PT - This is a package that provides alternative pseudo-random number generators.

mt_rand() is based upon the Mersenne Twister PRNG algorithm ([2 ], [1 ]). This PRNG has an enormous period (in fact it is 2**19937-1 which is a Mersenne Prime, hence the name). It is also supposedly faster than the standard C library rand() function. Furthermore, while the stock Tcl rand function generates numbers in the range (0,1), randmt produces numbers in the range [0,1]. If required, it can also produce values within (0,1) as well as integer values (any 32bit value)

isaac_rand() uses the ISAAC algorithm [3 ] by Bob Jenkins. ISAAC (Indirection, Shift, Accumulate, Add, and Count) generates 32-bit random numbers. The period is guaranteed to be at least 2**40 and is 2**8295 on average. The results are uniformly distributed, unbiased, and unpredictable unless you know the seed. This PRNG is suitable for cryptographic use provided a suitably random seed can be provided.

sub_rand() is a subtractive random number generator. See Knuth, D.E. 1981, "Seminumerical Algorithms" 2nd ed. vol 2 of "The Art of Computer Programming" para: 3.2-3.3.

For each of these PRNGs a command in the random namespace exists to provide extended access. Currently the subcommands are integer, double, seed and state which provide an integer or double random number (the expr function always provides a double). The seed subcommand lets you seed the generator with a byte array, for example an array obtained from /dev/random. The statesubcommand returns a byte array that contains the current state of the generator. This could be used to seed the next instance of a generator.


Currently the code is available at

 cvs -d:pserver:[email protected]:/cvsroot/tclsoap co Random

and the distributable files are at http://sourceforge.net/projects/tclsoap in the downloads section.


Usage

  package require Random

  # Seed and use the Mersenne Twister PRNG
  % expr {mt_srand(0)}
  0.548813502432
  % expr {mt_rand()}
  0.592844616527
  
  # Seed and use the ISAAC PRNG
  % expr {isaac_srand(0)}
  0.0943298905842
  % expr {isaac_rand()}
  0.187672290296
  
  # Subtractive PRNG
  % expr {sub_srand(0)}
  0.53392385
  % expr {sub_rand()}
  0.323008803

  # Extended ISAAC commands
  # Provide extra seed data (up to 256 values can be used)
  % ::random::isaac seed "\x01\x02\x03\x04\x05"
  
  # Get integers from the PRNG (must have been seeded already)
  % ::random::isaac integer
  1439929434
  
  # Get doubles (equivalent to the expr function)
  % ::random::isaac double
  0.960589176966

  # Extended subtractive PRNG commands
  % random::subtractive seed [string repeat \x00 3000]
  % random::subtractive integer
  533923850
  % string length [random::subtractive state]
  224

schlenk A little example generating random passwords with the help of the isaac generator:

 proc createPassword {{minlen 15}} {
        set chars [split {abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890?:!%$*} ""]
        set len [llength $chars]

        set pw ""
        while {[string length $pw] < $minlen} {        
                set rand [::random::isaac integer]
                while {$rand > 0 && $minlen && [string length $pw] < $minlen} {
                        set rest [expr {$rand % $len}]
                        set rand [expr {$rand / $len}]
                        append pw [lindex $chars $rest] 
                } 
        }
        return $pw
 }

random is also one of TclX Commands.

random limit

Return a random integer that is uniformly distributed over the range from 0 to one less than limit.

random seed ?seedval?

Reseed the random number generator with seedval, or a value based on the current system time if seedval is omitted.


2011-05-05 jima

For those like me who look for another rand() function, simpler perhaps than the one described here but readily available through expr command without needing to load this extension look in rand.