1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
| <?php
class GameRandomSystem { private $rng; private $seed; public function __construct($seed = null) { $this->seed = $seed ?? time(); $this->rng = new XorshiftFamily\Xorshift128($this->seed); $this->logSeed(); }
public function randomPosition($minX, $maxX, $minY, $maxY): array { return [ 'x' => $this->randomInt($minX, $maxX), 'y' => $this->randomInt($minY, $maxY) ]; }
public function weightedChoice(array $items, array $weights) { $totalWeight = array_sum($weights); $random = $this->randomFloat(0, $totalWeight); $cumulative = 0; foreach ($items as $i => $item) { $cumulative += $weights[$i]; if ($random <= $cumulative) { return $item; } } return end($items); }
public function shuffle(array $array): array { $result = $array; $count = count($result); for ($i = $count - 1; $i > 0; $i--) { $j = $this->randomInt(0, $i); list($result[$i], $result[$j]) = [$result[$j], $result[$i]]; } return $result; }
public function randomName(): string { $prefixes = ['A', 'Be', 'De', 'El', 'Fa', 'Jo', 'Ki', 'La', 'Ma', 'Na', 'O', 'Pa', 'Re', 'Si', 'Ta', 'Va']; $suffixes = ['an', 'ar', 'el', 'en', 'ia', 'ic', 'ie', 'in', 'is', 'on', 'or', 'us', 'yn', 'yth']; $prefix = $prefixes[$this->randomInt(0, count($prefixes) - 1)]; $suffix = $suffixes[$this->randomInt(0, count($suffixes) - 1)]; return $prefix . $suffix; } private function randomInt($min, $max): int { $range = $max - $min + 1; return $min + ($this->rng->nextInt() % $range); } private function randomFloat($min, $max): float { $range = $max - $min; return $min + ($this->rng->nextFloat() * $range); } }
?>
|