Since: 8.2

final class Randomizer (View source)

Provides a high-level API to the randomness provided by an Random\Engine.

Properties

Engine $engine

Methods

__construct(Engine|null $engine = null)

Constructs a new Randomizer

int
nextInt()

Get a positive integer

int
getInt(int $min, int $max)

Get a uniformly selected integer

string
getBytes(int $length)

Get random bytes

array
shuffleArray(array $array)

Get a permutation of an array

string
shuffleBytes(string $bytes)

Get a byte-wise permutation of a string

array
pickArrayKeys(array $array, int $num)

Select random array keys

array
__serialize()

Serializes the Randomizer object

void
__unserialize(array $data)

Deserializes the data parameter into a Randomizer object

float
nextFloat()

Get a float from the right-open interval [0.0, 1.0)

float
getFloat(float $min, float $max, IntervalBoundary $boundary = IntervalBoundary::ClosedOpen)

Get a uniformly selected float

string
getBytesFromString(string $string, int $length)

Get random bytes from a source string

Details

__construct(Engine|null $engine = null)

Constructs a new Randomizer

Parameters

Engine|null $engine

The Random\Engine to use to generate randomness. If engine is omitted or null, a new Random\Engine\Secure object will be used.

int nextInt()

Get a positive integer

Return Value

int

A positive integer between 0 and a maximum value depending on the number of bytes returned from Random\Engine::generate. The exact maximum can be calculated as 2$engine_bytes * 8 - 1 - 1.

Exceptions

RandomException

int getInt(int $min, int $max)

Get a uniformly selected integer

Parameters

int $min

The lowest value to be returned.

int $max

The highest value to be returned.

Return Value

int

A uniformly selected integer from the closed interval [min, max]. Both min and max are possible return values.

Exceptions

ValueError

string getBytes(int $length)

Get random bytes

Generates a string containing uniformly selected random bytes with the requested length.

Parameters

int $length

The length of the random string that should be returned in bytes; must be 1 or greater.

Return Value

string

A string containing the requested number of random bytes.

Exceptions

ValueError

array shuffleArray(array $array)

Get a permutation of an array

Returns a uniformly selected permutation of the input array.

Parameters

array $array

The array whose values are shuffled. The input array will not be modified.

Return Value

array

A permutation of the values of array. Array keys of the input array will not be preserved; the returned array will be a list (array_is_list).

string shuffleBytes(string $bytes)

Get a byte-wise permutation of a string

Returns a uniformly selected permutation of the input bytes.

Parameters

string $bytes

The string whose bytes are shuffled. The input string will not be modified.

Return Value

string

A permutation of the bytes of bytes.

array pickArrayKeys(array $array, int $num)

Select random array keys

Uniformly selects num distinct array keys of the input array.

Parameters

array $array

The array whose array keys are selected.

int $num

The number of array keys to return; must be between 1 and the number of elements in array.

Return Value

array

An array containing num distinct array keys of array. The returned array will be a list (array_is_list). It will be a subset of the array returned by array_keys.

Exceptions

ValueError

array __serialize()

Serializes the Randomizer object

Return Value

array

void __unserialize(array $data)

Deserializes the data parameter into a Randomizer object

Parameters

array $data

Return Value

void

No value is returned.

float nextFloat()

Since: 8.3

Get a float from the right-open interval [0.0, 1.0)

Returns a uniformly selected, equidistributed float from the right-open interval from 0.0 until, but not including, 1.0.

Return Value

float

float getFloat(float $min, float $max, IntervalBoundary $boundary = IntervalBoundary::ClosedOpen)

Since: 8.3

Get a uniformly selected float

Returns a uniformly selected, equidistributed float from a requested interval.

Parameters

float $min
float $max
IntervalBoundary $boundary

Return Value

float

Exceptions

ValueError

string getBytesFromString(string $string, int $length)

Since: 8.3

Get random bytes from a source string

Generates a string containing uniformly selected random bytes from the input string with the requested length.

Parameters

string $string
int $length

Return Value

string

Exceptions

ValueError