Next: Prime numbers, Previous: Mathematic 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.The random seed value can be given at the instantiation time by

`:seed`

initialization argument, or by using`mt-random-set-seed!`

described below.(define m (make <mersenne-twister> :seed (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). If it is a u32vector, up to 624 elements are used for initialization.Internally, MTRNG keeps its state in array of 32-bit integers. When a fixnum is given, its lower 32bit is used to generate a linear congruential series to fill the state space. If you do so, there is an algorithm that only samples a couple of result from MTRNG to calculate the original seed value, hence can predict entire sequence. 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-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.