<?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); } }
?>
|