Mersenne_Twister
in package
This provides a deterministic pseudo-random sequence that is seedable
Since PHP 5.2.1 mt_srand() and mt_rand() don't generate predictable sequences since PHP 5.2.1. The following code:
$code = 12345; $MT = new \Tsugi\Util\Mersenne_Twister($code); for($i=0; $i < 30; $i++ ) { $ran = $MT->getNext(0,6); echo $ran." "; }
Will always print out:
2 3 3 6 5 1 5 3 4 2 1 6 5 4 0 6 0 2 0 3 5 6 6 3 1 3 2 6 3 3
Note that this is written for a 64-bit system and will generate different sequences on 32 and 64 bit systems.
Table of Contents
Constants
- MAX : mixed = 0x8fffffff
Properties
Methods
- __construct() : mixed
- gaussian() : mixed
- Return a truncated Gaussian for this class
- getNext() : mixed
- Get the next pseudo-random number in the sequence
- setSeed() : mixed
- shuffle() : mixed
- Shuffle an array using this class
- generateTwister() : mixed
Constants
MAX
public
mixed
MAX
= 0x8fffffff
Properties
$index
private
mixed
$index
= 0
$state
private
mixed
$state
= array()
Methods
__construct()
public
__construct([mixed $seed = null ]) : mixed
Parameters
- $seed : mixed = null
gaussian()
Return a truncated Gaussian for this class
public
gaussian(int $max[, float $lambda = 0.2 ]) : mixed
Parameters
- $max : int
-
The maximum value we want to return - the returned value is in 0..$max.
- $lambda : float = 0.2
-
Controls the steepness of the curve (i.e. the average of the returned values). If this is 0.01 it is almost a uniform distribution with an average of about 0.4*$max; If this if this is 1.0, over 2/3 of the time this function will return 0 (i.e. a very quick drop-off). Values in the range 0.1 - 0.4 return gaussian distros that are distinctly exponential in drop-off and have a reasonably interesting average.
getNext()
Get the next pseudo-random number in the sequence
public
getNext([mixed $min = null ][, mixed $max = null ]) : mixed
Returns the next pseudo-random number in the range specified. The $max and $min are inclusive. If $max and $min are omitted a large integer is returned.
Parameters
- $min : mixed = null
-
The low end of the range (optional)
- $max : mixed = null
-
The high end of the range (optional)
setSeed()
public
setSeed(mixed $seed) : mixed
Parameters
- $seed : mixed
shuffle()
Shuffle an array using this class
public
shuffle(mixed $arr) : mixed
Parameters
- $arr : mixed
generateTwister()
private
generateTwister() : mixed