Next: Prime numbers, Previous: Mathematical constants, Up: Library modules - Utilities [Contents][Index]

`math.mt-random`

- Mersenne Twister Random number generator- Module:
**math.mt-random** -
Provides a pseudo random number generator (RNG) based on "Mersenne Twister" algorithm developed by Makoto Matsumoto and Takuji Nishimura. It is fast, and has huge period of 2^19937-1. See https://dl.acm.org/citation.cfm?id=272995, for details about the algorithm.

For typical use cases of random number generators, we recommend to use

`srfi-27`

which is implemented on top of this module and provides portable API. You should use this module directly only when you need functions that aren’t available through`srfi-27`

.

- Class:
**<mersenne-twister>** -
{

`math.mt-random`} A class to encapsulate the state of Mersenne Twister RNG. Each instance of this class has its own state, and can be used as an independent source of random bits if initialized by individual seed.

- Function:
**make-mersenne-twister***:optional seed private?* {

`math.mt-random`} Creates and returns a new`<mersenne-twister>`

RNG instance.If

`seed`argument is given, it must be either nonnegative exact integer, u32vector, or`#f`

. The key can be any length.If

`seed`is omitted or`#f`

, the RNG is initialized to the fixed value, so that it generates the same sequences of random numbers. If you want different sequence for each instance, you have to give different seeds explicitly, or initialize each instance with different seeds using`mt-random-set-seed!`

below.By default, the created RNG is thread-safe; you can call the generator from multiple thread without worrying to break its internal state.

However, if you know you only use it in a single thread, or you use separate mutex and know the generator call is alyways protected, then you can give a true value to

`private?`optional argument to avoid overhead of locking.(define m (make-mersenne-twister (sys-time))) (mt-random-real m) ⇒ 0.10284287848537865 (mt-random-real m) ⇒ 0.463227748348805 (mt-random-real m) ⇒ 0.8628500643709712 …

- Function:
**mt-random-set-seed!***mt seed* {

`math.mt-random`} Sets random seed value`seed`to the Mersenne Twister RNG (MTRNG)`mt`.`Seed`can be an arbitrary positive exact integer, or arbitrary length of u32vector (see Homogeneous vectors).Internally, MTRNG keeps its state in an array of 624 32-bit integers. If the given seed is small (e.g. fixnum), it can’t create enough variations. Particularly, if the seed is within 32bits, it can be computed by sampling a couple of result from MTRNG. If you want the random sequence harder to predict, prepare your own u32vector seed filled with high-entroby bits.

Note that Mersenne Twister is never intended for cryptograhpy, you shouldn’t use it for security-sensitive purposes.

NB: Up to 0.9.9, when

`seed`is a bignum, we roll our own way to fold it in 32bit integer and then called Mersenne-Twister’s initialization function. It loses the entropy, so we changed it and now all the bits in the bignum is used for the seed.

- Function:
**mt-random-get-seed***mt* {

`math.mt-random`} Returns the last seed value used to initialize Mersenne Twister RNG`mt`. It is either an exact integer or u32vector. If`mt`has never been initialized,`#<undef>`

is returned.

- Function:
**mt-random-get-state***mt* - Function:
**mt-random-set-state!***mt state* {

`math.mt-random`} Retrieves and reinstalls the state of Mersenne Twister RNG`mt`. The state is represented by a u32vector of 625 elements. The state can be stored elsewhere, and then restored to an instance of`<mersenne-twister>`

to continue to generate the pseudo random sequence.

- Function:
**mt-random-real***mt* - Function:
**mt-random-real0***mt* {

`math.mt-random`} Returns a random real number between 0.0 and 1.0. 1.0 is not included in the range.`Mt-random-real`

doesn’t include 0.0 either, while`mt-random-real0`

does. Excluding 0.0 is from the draft SRFI-27.

- Function:
**mt-random-integer***mt range* {

`math.mt-random`} Returns a random exact positive integer between 0 and`range`-1.`Range`can be any positive exact integer.

- Function:
**mt-random-fill-u32vector!***mt u32vector* - Function:
**mt-random-fill-f32vector!***mt f32vector* - Function:
**mt-random-fill-f64vector!***mt f64vector* {

`math.mt-random`} Fills the given uniform vector by the random numbers. For`mt-random-fill-u32vector!`

, the elements are filled by exact positive integers between 0 and 2^32-1. For`mt-random-fill-f32vector!`

and`mt-random-fill-f64vector!`

, it is filled by an inexact real number between 0.0 and 1.0, exclusive.If you need a bunch of random numbers at once, these are much faster than getting one by one.

DRAFT