| Next revision | Previous revision |
| scriptlog:lib:core:random [2026/09/26 09:09] – admin admin | scriptlog:lib:core:random [2026/09/26 09:34] (current) – admin admin |
|---|
| ======= Docblock Metadata ======= | ======= Docblock Metadata ======= |
| |
| ^ Tag ^ Value ^ | ^ Tag ^ Value ^ |
| | ''@category'' | Core Class | | | ''@category'' | Core Class | |
| | ''@author'' | Anthony Ferrara [email protected] | | | ''@author'' | Anthony Ferrara [email protected] | |
| | ''@see'' | https://github.com/ircmaxell/random_compat | | | ''@see'' | https://github.com/ircmaxell/random_compat | |
| | ''@see'' | https://timoh6.github.io/2013/11/05/Secure-random-numbers-for-PHP-developers.html | | | ''@see'' | https://timoh6.github.io/2013/11/05/Secure-random-numbers-for-PHP-developers.html | |
| |
| ======= Inheritance ======= | ======= Inheritance ======= |
| ======= Constants (5) ======= | ======= Constants (5) ======= |
| |
| ^ Visibility ^ Name ^ Value ^ Line ^ | ^ Visibility ^ Name ^ Value ^ Line ^ |
| | ''-'' | ''BLOCK_SIZE'' | ''32;'' | 19 | | | ''-'' | ''BLOCK_SIZE'' | ''32;'' | 19 | |
| | ''-'' | ''TOKEN_LOWER_ALPHA'' | ''"abcdefghijklmnopqrstuvwxyz";'' | 21 | | | ''-'' | ''TOKEN_LOWER_ALPHA'' | ''"abcdefghijklmnopqrstuvwxyz";'' | 21 | |
| | ''-'' | ''TOKEN_UPPER_ALPHA'' | ''"ABCDEFGHIJKLMNOPQRSTUVWXYZ";'' | 22 | | | ''-'' | ''TOKEN_UPPER_ALPHA'' | ''"ABCDEFGHIJKLMNOPQRSTUVWXYZ";'' | 22 | |
| | ''-'' | ''TOKEN_ALPHA'' | ''"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";'' | 23 | | | ''-'' | ''TOKEN_ALPHA'' | ''"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";'' | 23 | |
| | ''-'' | ''TOKEN_ALNUM'' | ''"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";'' | 24 | | | ''-'' | ''TOKEN_ALNUM'' | ''"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";'' | 24 | |
| |
| ======= Properties (2) ======= | ======= Properties (2) ======= |
| |
| ^ Visibility ^ Type ^ Name ^ Default ^ Line ^ | ^ Visibility ^ Type ^ Name ^ Default ^ Line ^ |
| | ''private static'' | ''static'' | ''$state'' | '''';'' | 26 | | | ''private static'' | ''static'' | ''$state'' | '''''' | 26 | |
| | ''protected'' | ''(untyped)'' | ''$secure'' | ''false;'' | 28 | | | ''protected'' | ''(untyped)'' | ''$secure'' | ''false'' | 28 | |
| |
| ======= Methods (12) ======= | ======= Methods (12) ======= |
| |
| ^ Visibility ^ Method ^ Summary ^ Line ^ | ^ Visibility ^ Method ^ Summary ^ Line ^ |
| | public | ''__construct()'' | Construct a new random number generator | 35 | | | public | ''__construct()'' | Construct a new random number generator | 35 | |
| | public | ''bytes()'' | Generate a native binary string | 47 | | | public | ''bytes()'' | Generate a native binary string | 47 | |
| | public | ''int()'' | Generate a random integer | 66 | | | public | ''int()'' | Generate a random integer | 66 | |
| | public | ''float()'' | Generate a random floating point number between 0 and 1.0 | 94 | | | public | ''float()'' | Generate a random floating point number between 0 and 1.0 | 94 | |
| | public | ''choose()'' | Given a sequence (string, array or object), pick one of them at random | 106 | | | public | ''choose()'' | Given a sequence (string, array or object), pick one of them at random | 106 | |
| | public | ''shuffle()'' | Given a sequence(string, array or object), generate a shuffled sequence | 134 | | | public | ''shuffle()'' | Given a sequence(string, array or object), generate a shuffled sequence | 134 | |
| | public | ''token()'' | Generate a token with the specified alphabet | 178 | | | public | ''token()'' | Generate a token with the specified alphabet | 178 | |
| | protected | ''genRandom()'' | Protected function to overload the generator for unit testing purposes | 201 | | | protected | ''genRandom()'' | Protected function to overload the generator for unit testing purposes | 201 | |
| | private | ''genNormal()'' | Generate a non-CS random seed | 214 | | | private | ''genNormal()'' | Generate a non-CS random seed | 214 | |
| | private | ''genSecure()'' | Generate a cryptographically-secure random seed | 242 | | | private | ''genSecure()'' | Generate a cryptographically-secure random seed | 242 | |
| | private | ''mergeBuffers()'' | Merge two buffers into a single buffer in a non-deterministic way | 273 | | | private | ''mergeBuffers()'' | Merge two buffers into a single buffer in a non-deterministic way | 273 | |
| | private | ''countBits()'' | Count the number of bits needed to represent an integer | 285 | | | private | ''countBits()'' | Count the number of bits needed to represent an integer | 285 | |
| |
| |
| Construct a new random number generator | Construct a new random number generator |
| |
| ^ Parameter ^ Type ^ Default ^ Description ^ | ^ Parameter ^ Type ^ Default ^ Description ^ |
| | ''$secure'' | ''(untyped)'' | ''false'' | Should generated random numbers be cryptographically secure | | | ''$secure'' | ''(untyped)'' | ''false'' | Should generated random numbers be cryptographically secure | |
| |
| |
| Generate a native binary string | Generate a native binary string |
| |
| ^ Parameter ^ Type ^ Default ^ Description ^ | ^ Parameter ^ Type ^ Default ^ Description ^ |
| | ''$length'' | ''(untyped)'' | //required// | The length of the string to generate | | | ''$length'' | ''(untyped)'' | //required// | The length of the string to generate | |
| |
| **Returns:** ''(none declared)'' — string The generated string | **Returns:** ''(none declared)'' — string The generated string |
| Generate a random integer | Generate a random integer |
| |
| ^ Parameter ^ Type ^ Default ^ Description ^ | ^ Parameter ^ Type ^ Default ^ Description ^ |
| | ''$min'' | ''(untyped)'' | //required// | The starting point, or if max is omitted the ending point | | | ''$min'' | ''(untyped)'' | //required// | The starting point, or if max is omitted the ending point | |
| | ''$max'' | ''(untyped)'' | ''null'' | The ending point | | | ''$max'' | ''(untyped)'' | ''null'' | The ending point | |
| |
| **Returns:** ''(none declared)'' — int The generated integer | **Returns:** ''(none declared)'' — int The generated integer |
| Given a sequence (string, array or object), pick one of them at random | Given a sequence (string, array or object), pick one of them at random |
| |
| ^ Parameter ^ Type ^ Default ^ Description ^ | ^ Parameter ^ Type ^ Default ^ Description ^ |
| | ''$value'' | ''(untyped)'' | //required// | The sequence to choose from | | | ''$value'' | ''(untyped)'' | //required// | The sequence to choose from | |
| |
| **Returns:** ''(none declared)'' — mixed The randomly chosen value | **Returns:** ''(none declared)'' — mixed The randomly chosen value |
| Note: for strings, the return is a string. For arrays and objects, it's an array | Note: for strings, the return is a string. For arrays and objects, it's an array |
| |
| ^ Parameter ^ Type ^ Default ^ Description ^ | ^ Parameter ^ Type ^ Default ^ Description ^ |
| | ''$value'' | ''(untyped)'' | //required// | The sequence to choose from | | | ''$value'' | ''(untyped)'' | //required// | The sequence to choose from | |
| |
| **Returns:** ''(none declared)'' — string|array The generated shuffled sequence | **Returns:** ''(none declared)'' — string|array The generated shuffled sequence |
| Generate a token with the specified alphabet | Generate a token with the specified alphabet |
| |
| ^ Parameter ^ Type ^ Default ^ Description ^ | ^ Parameter ^ Type ^ Default ^ Description ^ |
| | ''$length'' | ''(untyped)'' | //required// | The length of the token to generate | | | ''$length'' | ''(untyped)'' | //required// | The length of the token to generate | |
| | ''$alphabet'' | ''(untyped)'' | ''self::TOKEN_ALNUM'' | The "alphabet" (characters) to use | | | ''$alphabet'' | ''(untyped)'' | ''self::TOKEN_ALNUM'' | The "alphabet" (characters) to use | |
| |
| **Returns:** ''(none declared)'' — string The generated random token | **Returns:** ''(none declared)'' — string The generated random token |
| Merge two buffers into a single buffer in a non-deterministic way | Merge two buffers into a single buffer in a non-deterministic way |
| |
| ^ Parameter ^ Type ^ Default ^ Description ^ | ^ Parameter ^ Type ^ Default ^ Description ^ |
| | ''$buffer1'' | ''(untyped)'' | //required// | //none// | | | ''$buffer1'' | ''(untyped)'' | //required// | //none// | |
| | ''$buffer2'' | ''(untyped)'' | //required// | //none// | | | ''$buffer2'' | ''(untyped)'' | //required// | //none// | |
| |
| **Returns:** ''(none declared)'' | **Returns:** ''(none declared)'' |
| Count the number of bits needed to represent an integer | Count the number of bits needed to represent an integer |
| |
| ^ Parameter ^ Type ^ Default ^ Description ^ | ^ Parameter ^ Type ^ Default ^ Description ^ |
| | ''$number'' | ''(untyped)'' | //required// | //none// | | | ''$number'' | ''(untyped)'' | //required// | //none// | |
| |
| **Returns:** ''(none declared)'' | **Returns:** ''(none declared)'' |