Last active
November 29, 2019 01:47
-
-
Save necrogami/1df1c9e8aad0618e8ef02b15fba86c28 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| class Dice | |
| { | |
| protected $json; | |
| protected $hex; | |
| /** | |
| * This sets how many hex characters to use for the length of the number | |
| * | |
| * 1 = 0-15 | |
| * 2 = 0-255 | |
| * 3 = 0-4096 | |
| * 4 = 0-65535 -- this | |
| * 5 = 0-1048575 | |
| * 6 = 0-16777215 | |
| */ | |
| protected $length; | |
| public function __construct($length = null) | |
| { | |
| if (is_null($length)) { | |
| $length = 4; | |
| } | |
| $this->length = $length; | |
| $this->getJson(); | |
| $this->processNumbers(); | |
| } | |
| protected function getJson() | |
| { | |
| if (!file_exists('numbers.json')) { | |
| echo "Aquiring new random numbers" . PHP_EOL; | |
| $data = file_get_contents('https://qrng.anu.edu.au/API/jsonI.php?length=1024&type=hex16&size=1024'); | |
| file_put_contents('numbers.json', $data); | |
| $this->json = json_decode($data); | |
| } | |
| { | |
| $this->json = json_decode(file_get_contents('numbers.json')); | |
| } | |
| return $this->json; | |
| } | |
| protected function processNumbers() | |
| { | |
| if (!file_exists('numbers.data')) { | |
| $this->hex = implode($this->json->data); | |
| $this->hex = str_split($this->hex, $this->length); | |
| foreach ($this->hex as $key => $value) { | |
| $this->hex[$key] = hexdec($value); | |
| } | |
| file_put_contents('numbers.data', serialize($this->hex)); | |
| } else { | |
| $this->hex = unserialize(file_get_contents('numbers.data')); | |
| if (empty($this->hex)) { | |
| unlink('numbers.data'); | |
| unlink('numbers.json'); | |
| $this->getJson(); | |
| $this->processNumbers(); | |
| } | |
| } | |
| return $this->hex; | |
| } | |
| public function getNumber() | |
| { | |
| if (count($this->hex) == 0) { | |
| $this->processNumbers(); | |
| } | |
| $number = array_shift($this->hex); | |
| file_put_contents('numbers.data', serialize($this->hex)); | |
| return $number; | |
| } | |
| public function rand($min = null, $max = null) | |
| { | |
| if (!is_null($max) && $max > $this->rand_max()) { | |
| throw new Exception("Max cannot be greater then " . $this->rand_max(), 1); | |
| } | |
| if (!is_null($min) && $min > $this->rand_max()) { | |
| throw new Exception("Min cannot be greater then " . $this->rand_max(), 1); | |
| } | |
| if (!is_null($min) && !is_null($max)) { | |
| $counts = []; | |
| $count = 0; | |
| for ($i = $min; $i <= $max; $i++) { | |
| $counts[$count] = $i; | |
| $count++; | |
| } | |
| $countKey = $this->getNumber() % $sides; | |
| $value = $counts[$countKey]; | |
| return $value; | |
| } else { | |
| return $this->getNumber(); | |
| } | |
| } | |
| public function rand_max() | |
| { | |
| $str = ""; | |
| for ($i=1; $i <= $this->length ; $i++) { | |
| $str .= "f"; | |
| } | |
| return hexdec($str); | |
| } | |
| public function roll($sides) | |
| { | |
| return ($this->getNumber() % $sides)+1; | |
| } | |
| } | |
| $dice = new Dice(); | |
| for ($i=0; $i < 100; $i++) { | |
| print_r($dice->roll(20)); | |
| echo PHP_EOL; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment