php8.5
Home/ Manual/ engine / pcgoneseq128xslrr64/ Random\Engine\PcgOneseq128XslRr64::__construct

Random\Engine\PcgOneseq128XslRr64::__construct

PHP function Edit on GitHub ✎

(PHP 8 >= 8.2.0)

Constructs a new PCG Oneseq 128 XSL RR 64 engine

Description

Random\Engine\PcgOneseq128XslRr64::__construct(string|int|null $seed = null)

Parameters

seed

How the internal 128 bit (16 byte) state consisting of one unsigned 128 bit integer is seeded depends on the type used as the seed.

TypeDescription
nullFills the state with 16 random bytes generated using the CSPRNG.
intFills the state by setting the state to 0, advancing the engine one step, adding the value of seed interpreted as an unsigned 64 bit integer, and advancing the engine another step.
stringFills the state by interpreting a 16 byte string as a little-endian unsigned 128 bit integer.

Errors/Exceptions

  • If the length of a string seed is not 16 bytes, a ValueError will be thrown.

Examples

Random\Engine\PcgOneseq128XslRr64::__construct() example

php
<?php
// Uses a random 128 Bit seed.
$e = new \Random\Engine\PcgOneseq128XslRr64();

$r = new \Random\Randomizer($e);
?>

Deriving a seed from a String

php
<?php
$string = "My string seed";

// Hash the string with truncated SHA-256 using binary output
// to turn the $string into a 128 Bit seed. Using the same
// string will result in the same sequence of randomness.
$e = new \Random\Engine\PcgOneseq128XslRr64(
    substr(hash('sha256', $string, binary: true), 0, 16)
);

echo bin2hex($e->generate()), "\n";
?>

The above example will output:

output
8333ef59315b16d8

Source: reference/random/random/engine/pcgoneseq128xslrr64/construct.xml · from the official PHP manual (php/doc-en)