For Gauche 0.9.5

Next: , Previous: , Up: Library modules - Utilities   [Contents][Index]

12.25 - Mersenne Twister Random number generator


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 MT, 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>

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

Sets random seed value seed to the Mersenne Twister RNG mt. Seed can be an arbitrary positive exact integer, or arbitrary length of u32vector (see Homogeneous vectors). If it is an integer, the lower 32bits are used for initialization. If it is a u32vector, up to 624 elements are used for initialization.

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

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

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

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

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.

Next: , Previous: , Up: Library modules - Utilities   [Contents][Index]