Skip to content

Core\Random\Seeded::int

$seeded->int(int $min, int $max): int$seeded->int(
    int $min,
    int $max,
): int

Returns a whole number between two numbers from a seeded generator. Both numbers can be returned.

You make the generator with new Core\Random\Seeded(42). The number 42 is the seed. Two generators with the same seed return the same numbers in the same order, on every run and on every computer. This makes a test or a simulation repeatable. $generator->int(1, 6) rolls a die, and each value from 1 to 6 comes up equally often.

The first number must not be larger than the second. If it is, the method throws an error.

Good to know: anybody who knows the seed knows every number. For a login code or a PIN, use Core\Random::int.

Parameters

  • $minintThe lowest value the result can be.
  • $maxintThe highest value the result can be.

Return value

Returns int. An int from $min to $max. When the two are equal, the result is $min.

Errors

  • RuntimeError$min is greater than $max. The method does not swap the two values.

Examples

The same seed gives the same rolls

<?nvs
// Shows that two generators with the same seed roll the same dice.
Core\Random\Seeded $first = new Core\Random\Seeded(42);
Core\Random\Seeded $second = new Core\Random\Seeded(42);
// Each generator rolls a die four times. Both lines are the same on every run.
echo $first->int(1, 6), ' ', $first->int(1, 6), ' ', $first->int(1, 6), ' ', $first->int(1, 6), "\n";
echo $second->int(1, 6), ' ', $second->int(1, 6), ' ', $second->int(1, 6), ' ', $second->int(1, 6), "\n";
// A different seed gives different rolls.
Core\Random\Seeded $other = new Core\Random\Seeded(7);
echo $other->int(1, 6), ' ', $other->int(1, 6), ' ', $other->int(1, 6), ' ', $other->int(1, 6), "\n";
Output
5 1 2 3
5 1 2 3
3 1 6 4

Bounds in the wrong order

<?nvs
// Shows what happens when the two bounds are the same, and when they are in the wrong order.
Core\Random\Seeded $generator = new Core\Random\Seeded(42);
// When both bounds are 5, the result is 5. This prints 5.
echo $generator->int(5, 5), "\n";
// A lower bound above the upper bound throws an error. The bounds are not swapped.
try {
echo $generator->int(10, 1), "\n";
} catch (Throwable $e) {
echo 'Error: ', $e->message, "\n";
}
Output
5
Error: Core\Random\Seeded::int(): the range is empty — the lower bound 10 is above the upper bound 1, and both ends are inclusive

Test orders you can repeat

<?nvs
// Shows how to make test data that is the same on every run.
// A test needs ten orders with a random quantity and price. The seed is fixed,
// so the test sees the same ten orders every time it runs.
Core\Random\Seeded $generator = new Core\Random\Seeded(2024);
int $total = 0;
int $order = 1;
while ($order <= 10) {
int $quantity = $generator->int(1, 5);
int $cents = $generator->int(199, 4999);
echo 'order ', $order, ': ', $quantity, ' x ', $cents, " cents\n";
$total = $total + $quantity * $cents;
$order = $order + 1;
}
// The test can check this total, because it never changes.
echo 'total: ', $total, " cents\n";
Output
order 1: 4 x 665 cents
order 2: 2 x 756 cents
order 3: 5 x 2851 cents
order 4: 1 x 3062 cents
order 5: 1 x 2248 cents
order 6: 3 x 4217 cents
order 7: 5 x 3374 cents
order 8: 1 x 3505 cents
order 9: 5 x 1826 cents
order 10: 5 x 2341 cents
total: 77598 cents