public function testDistribution()
 {
     $twister = new MersenneTwister();
     $twister->seed(424242);
     $bins = array_fill(0, 2, 0);
     for ($i = 0; $i < 1000; $i++) {
         $number = $twister->extractNumber();
         $bin = (int) floor($number / pow(2, 31));
         $bins[$bin]++;
     }
     foreach ($bins as $count) {
         $this->assertGreaterThan(400, $count);
     }
 }
示例#2
0
/**
 * Shuffle with seed
 * @param array $arr
 * @param $seed
 * @return array
 */
function shuffle($arr, $seed = -1)
{
    if ($seed == -1) {
        return $arr;
    }
    $mt = new MersenneTwister($seed);
    $new = $arr;
    for ($i = count($new) - 1; $i > 0; $i--) {
        $j = $mt->getNext(0, $i);
        $tmp = $new[$i];
        $new[$i] = $new[$j];
        $new[$j] = $tmp;
    }
    return $new;
}