PHP 8.3: Random extension: New \Random\Randomizer::getFloat() and nextFloat() methods

Version8.3
TypeNew Feature

The Random extension in PHP 8.3 adds \Random\Randomizer::getFloat() and \Random\Randomizer::nextFloat() methods that generate a random float value.

There is also a new \Random\IntervalBoundary Enum that can be used as a parameter for the getFloat method to indicate whether the $min and $max boundaries should be inclusive or not.

\Random\Randomizer::getFloat() method

namespace Random;

class Randomizer {
    // ...
    public function getFloat(
        float $min,
        float $max,
        \Random\IntervalBoundary $boundary = \Random\IntervalBoundary::ClosedOpen
    ): float {

    }
}
  • $max must be larger than $min parameter. Otherwise a \ValueError exception will be thrown:

    ValueError: Random\Randomizer::getFloat(): Argument #2 ($max) must be greater than argument #1 ($min)
  • $min and $max must be finite. PHP throws a \ValueError exception otherwise:

    ValueError: Random\Randomizer::getFloat(): Argument #1 ($min) must be finite

\Random\IntervalBoundary Enum

\Random\Randomizer::getFloat() method accepts a \Random\IntervalBoundary Enum as the third parameter to indicate whether $min and $max values must be inclusive or not.

enum IntervalBoundary {
    case ClosedOpen;
    case ClosedClosed;
    case OpenClosed;
    case OpenOpen;
}
  • IntervalBoundary::ClosedOpen: $min may be returned, $max may not.
  • IntervalBoundary::ClosedClosed: $min and $max both may be returned.
  • IntervalBoundary::OpenClosed: $min may not be returned, $max may.
  • IntervalBoundary::OpenOpen: Neither $min, nor $max may be returned.

Usage Examples

Generate a random float between 0 < and < 10.

$rng = new Random\Randomizer();
$rng->getFloat(0, 10, \Random\IntervalBoundary::OpenOpen); // 9.3835746900717
$rng->getFloat(0, 10, \Random\IntervalBoundary::OpenOpen); // 3.065611591453

***

Generate a random `float` between `42 <=` and `< 43`.

```php
$rng = new Random\Randomizer();

// IntervalBoundary::ClosedOpen is the default $boundary parameter value.
$rng->getFloat(42, 43); // 42.777167603068
$rng->getFloat(42, 43); // 42.565307732356

\Random\Randomizer::nextFloat() method

The new \Random\Randomizer::nextFloat() method is functionally identical to \Random\Randomizer::getFloat(0, 1, \Random\IntervalBoundary::ClosedOpen).

JavaScript Math.random() is also similar to \Random\Randomizer::nextFloat(), as both of them return a random value in the range 0 <= and < 1.

namespace Random;

class Randomizer {
    // ...
    public function nextFloat(): float {
    }
}

Usage Examples

Generate a random number in the range 0 <= and < 1:

$rng = new Random\Randomizer();
$rng->nextFloat(); // 0.21185336351144

Related Changes


Backwards Compatible Impact

\Random\Randomizer class is declared final, which means it cannot be extended. Unless a user-land \Random\IntervalBoundary class or an Enum is declared, this change should not cause any backwards compatibility issues.


RFC Discussion Implementation