protected function replaceHashValueRange($value, $min, $max)
 {
     if (stripos($value, 'H') !== false) {
         $twister = new twister();
         $twister->init_with_string($this->hash_data);
         $value = str_ireplace('H', $twister->rangeint($min, $max), $value);
     }
     return $value;
 }
示例#2
0
 public function testRangeIntWorksOnWidest64BitRange()
 {
     if (PHP_INT_SIZE !== 8) {
         $this->markTestSkipped('not a 64-bit system');
     }
     #> Given
     $twister = new twister(42);
     /*<
         The Mersenne Twister is deterministic, so we can initialise it
         with any value that makes the assertion below true.
       */
     #> When
     $rand = $twister->rangeint(-PHP_INT_MAX - 1, PHP_INT_MAX);
     #> Then
     $this->assertNotEquals($rand, 0);
 }
示例#3
0
for ($i = 0; $i < $num_iters; $i++) {
    $sum += $twister3->rangereal_halfopen(10, 20);
}
/*
  the call to rangereal_halfopen produces a
  floating-point number >= 10 and < 20
*/
print "This is the average, " . "which should be about 15: " . $sum / $num_iters . "\n";
$twister3 = unserialize($saved);
# run the loop again
#
$sum = 0;
for ($i = 0; $i < $num_iters; $i++) {
    $sum += $twister3->rangereal_halfopen(10, 20);
}
print "This is the average again, " . "which should be the same as before: " . $sum / $num_iters . "\n";
#--------------------------------------------
$twister4 = new twister();
$twister4->init_with_file("/dev/urandom", twister::N);
/*
  This reads characters from /dev/urandom and
  uses them to initialise the random number
  generator.

  The second argument is multiplied by 4 and
  then used as an upper bound on the number of
  characters to read.
*/
if ($twister4->rangeint(1, 6) == 6) {
    print "You've won -- congratulations!\n";
}