3v4l.org

run code in 300+ PHP versions simultaneously
<?php class ProceduralNumericSequenceGenerator { private $algo, $state; public function __construct($seed = NULL, $algo = "sha256") { if ($seed === NULL) { $seed = time(); } if (array_search($algo, hash_algos()) === FALSE) { throw new RuntimeException("Hashing algorithm \"$algo\" is not available."); } $this->state = $seed; $this->algo = $algo; } public function getInt() { return hexdec(substr($this->state, 0, PHP_INT_SIZE)); } public function advance() { $this->state = hash($this->algo, $this->state); } public function advanceNew() { $new = clone $this; $new->advance(); return $new; } public function advanceAndGetInt() { $this->advance(); return $this->getInt(); } public function advanceNewAndGetInt() { $new = $this->advanceNew(); return [$new->getInt(), $new]; } } $rng = new ProceduralNumericSequenceGenerator(time()); var_dump($rng->advanceAndGetInt()); $rng2 = clone $rng; var_dump($rng->advanceAndGetInt()); var_dump($rng2->advanceAndGetInt());

preferences:
51.49 ms | 1808 KiB | 5 Q