public function testNextSample() { $sampler = new GaussianSampler(Random::create()); $data = []; for ($i = 0; $i < 50000; $i++) { $data[] = $sampler->nextSample(); } $c = count($data); // Average $avg = array_sum($data) / $c; $this->assertTrue(abs($avg) < 0.02); // Median sort($data); $median = $data[(int) ($c / 2)]; $this->assertTrue(abs($median) < 0.02); // Mode $stats = []; foreach ($data as $num) { $k = (string) (0.0 + sprintf('%.1f', $num)); if (!isset($stats[$k])) { $stats[$k] = 0; } $stats[$k]++; } arsort($stats); $key = (double) key($stats); $this->assertTrue($key >= -0.2 && $key <= 0.2); $this->assertTrue($stats['0'] > $stats['3']); $a1 = $stats['0.1'] + $stats['-0.1'] + $stats['0.2'] + $stats['-0.2'] + $stats['0.3'] + $stats['-0.3']; $a2 = $stats['0.4'] + $stats['-0.4'] + $stats['0.5'] + $stats['-0.5'] + $stats['0.6'] + $stats['-0.6']; $this->assertTrue($a1 > $a2); // Simple probability distribution test $stats = []; foreach ($data as $num) { $k = ceil(abs($num)); if (!isset($stats[$k])) { $stats[$k] = 0; } $stats[$k]++; } $k = 100 / $c; $p1 = round($k * $stats[1]); $p2 = round($k * ($stats[2] + $stats[1])); $p3 = round($k * ($stats[3] + $stats[2] + $stats[1])); // 68% of the data should be within one standard deviation $this->assertTrue($p1 >= 67 && $p1 <= 69); // 95% of the data should be within two standard deviations $this->assertTrue($p2 >= 94 && $p1 <= 96); // 99% of the data should be within three standard deviations $this->assertTrue($p3 >= 98); }
public function testCreateFromState() { /** @var \Savvot\Random\AbstractRand $rnd */ $rnd = Random::create(null, Random::HASH); $rnd->random(); $rnd->random(); $rnd->random(); $state = $rnd->getState(); $seq1 = []; for ($i = 0; $i < 100; $i++) { $seq1[] = $rnd->random(0, $i); } $rnd = Random::createFromState($state); $this->assertInstanceOf('\\Savvot\\Random\\HashRand', $rnd); $seq1test = []; for ($i = 0; $i < 100; $i++) { $seq1test[] = $rnd->random(0, $i); } $this->assertSame($seq1, $seq1test); }