rng - Pseudo random number generator module

Module Description

The rng module implements a pseudo random number generator; it uses the Mersenne Twister as generator with a period of 2^19937 - 1.

Module Words

Random generator structure

rng% ( -- n )
Get the required space for a rng variable

Random generator creation, initialisation and destruction

rng-init ( u rng -- )
Initialise the generator with the seed u

rng-create ( u "<spaces>name" -- ; -- rng )
Create a named random generator in the dictionary with seed u

rng-new ( u -- rng )
Create a new random generator on the heap with seed u

rng-free ( rng -- )
Free the random generator from the heap

Random generator words

rng-seed ( u rng -- )
Initialise the generator with the seed u

rng-next-number ( rng -- n )
Calculate the next pseudo random number, 32 bit

rng-next-float ( rng -- r )
Calculate the next pseudo random float number, range [0,1>

Inspection

rng-dump ( rng -- )
Dump the random generator

Examples

include ffl/rng.fs

\ Create a random generator variable in the dictionary

5489 rng-create rng1

\ Generate a random number

rng1 rng-next-number . cr

\ Generate a floating random number [0,1>

rng1 rng-next-float fs. cr


\ Create a random generator variable on the heap

5489 rng-new value rng2 

\ Change the seed

19650218 rng2 rng-seed

\ Generate a random number

rng2 rng-next-number . cr

\ Free the variable from the heap

rng2 rng-free


generated 10-Apr-2008 by ofcfrth-0.5.0