test(compare): trace deterministic monthly seed progression

This commit is contained in:
2026-08-03 18:57:01 +00:00
parent 3b2fd1082f
commit f16a6b6880
23 changed files with 1274 additions and 213 deletions
+34 -5
View File
@@ -4,13 +4,36 @@ namespace sammo;
class RandUtil
{
private ?int $traceGeneralId = null;
private int $traceSequence = 0;
public function __construct(public readonly RNG $rng)
{
}
public function setTraceGeneralId(?int $generalId): void
{
$this->traceGeneralId = $generalId;
}
private function trace(string $method, mixed $result): void
{
if ($this->traceGeneralId === null) {
return;
}
fwrite(STDOUT, 'AI_RNG_TRACE ' . json_encode([
'generalId' => $this->traceGeneralId,
'sequence' => $this->traceSequence++,
'method' => $method,
'result' => $result,
], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . "\n");
}
public function nextFloat1(): float
{
return $this->rng->nextFloat1();
$result = $this->rng->nextFloat1();
$this->trace('nextFloat1', $result);
return $result;
}
public function nextRange(int|float $min, int|float $max): float
@@ -25,16 +48,22 @@ class RandUtil
if ($range > $this->rng->getMaxInt()) {
throw new \InvalidArgumentException("Invalid random int range");
}
return $this->rng->nextInt($range) + $min;
$result = $this->rng->nextInt($range) + $min;
$this->trace('nextRangeInt', $result);
return $result;
}
public function nextInt(?int $max = null): int{
return $this->rng->nextInt($max);
$result = $this->rng->nextInt($max);
$this->trace('nextInt', $result);
return $result;
}
public function nextBit(): bool
{
return $this->rng->nextBits(1) !== "\0";
$result = $this->rng->nextBits(1) !== "\0";
$this->trace('nextBit', $result);
return $result;
}
public function nextBool(int|float $prob = 0.5): bool
@@ -97,7 +126,7 @@ class RandUtil
if(!$keys){
throw new \InvalidArgumentException();
}
$keyIdx = $this->rng->nextInt(count($keys) - 1);
$keyIdx = $this->nextInt(count($keys) - 1);
return $items[$keys[$keyIdx]];
}