PHP Luminova: Generate Random Values and Barcode Checksums
Generate deterministic identifiers, random bytes, integers, strings, passwords, and barcode values. Calculate check digits and GMN check characters.
Choose bytes() when you need an exact count of raw random bytes and hex() for their printable hexadecimal representation. Use generate() for text drawn from a chosen character set. Barcode helpers create candidate strings. For EAN-13 and UPC-A, verify the check digit with an independent GS1 validator before use. The current weighting starts at 1 for the rightmost payload digit, which can yield a different digit from the standard weighting.
Usage
use Luminova\Utility\Random;
$hex = Random::hex(16); // 32 hexadecimal characters
$code = Random::generate(8, Random::TYPE_ALPHANUMERIC, uppercase: true);
$number = Random::integer('1', '100'); // inclusive boundsClass Definition
- Full namespace:
Luminova\Utility\Random - This class is final.
Constants
TYPE_ALPHABET, TYPE_GMN, TYPE_INTEGER, TYPE_CHARACTER, TYPE_ALPHANUMERIC, TYPE_PASSWORD, TYPE_BYTES, TYPE_HEX, and TYPE_DEFAULT select output for generate().
The integer bounds are INT8_MAX, UINT8_MAX, INT16_MIN, INT16_MAX, UINT16_MAX, INT32_MIN, INT32_MAX, UINT32_MIN, UINT32_MAX, INT64_MIN, INT64_MAX, UINT64_MIN, and UINT64_MAX. INT64_MAX uses PHP_INT_MAX. The signed 64-bit minimum and unsigned 64-bit bounds are stored as strings.
Methods
identifier
Generate a repeatable identifier by hashing the trimmed source, format it in uppercase, and group it with hyphens when requested. GMP changes the representation from hexadecimal to Base36 when available, so generate and verify with the same runtime capabilities.
public static function identifier(
string $from,
int $length = 23,
int $group = 5,
string $algo = 'sha256'
): stringParameters:
| Parameter | Type | Description |
|---|---|---|
$from | string | Source value to hash. |
$length | int | Requested identifier length from 4 to 64 (default: 23). |
$group | int | Group size. Use zero to skip grouping (default: 5). |
$algo | string | Hash algorithm (default: 'sha256'). |
Return:
string - Formatted identifier.
Throws:
- Luminova\Exceptions\InvalidArgumentException if the requested length is outside the allowed range or the hash algorithm is unsupported.
See Validator::isIdentifier() to verify a generated identifier.
Examples:
use Luminova\Utility\Random;
echo Random::identifier('order-1001');bytes
Generate random bytes. In monotonic mode, calls in the same millisecond can increment the previous byte sequence. Keep the requested length consistent across calls in that mode.
public static function bytes(
int $length = 10,
bool $monotonic = false,
?int &$timestamp = null
): stringParameters:
| Parameter | Type | Description |
|---|---|---|
$length | int | Number of source bytes (default: 10). |
$monotonic | bool | Whether to enable monotonic generation within the same process (default: false). |
$timestamp | int\|null | Output variable assigned the millisecond timestamp in monotonic mode and null otherwise (default: null). |
Return:
string - Raw bytes.
hex
Return a hexadecimal representation of the bytes produced by bytes().
public static function hex(
int $length = 10,
bool $monotonic = false,
?int &$timestamp = null
): stringParameters:
| Parameter | Type | Description |
|---|---|---|
$length | int | Number of source bytes (default: 10). |
$monotonic | bool | Whether to enable monotonic generation within the same process (default: false). |
$timestamp | int\|null | Output variable assigned the millisecond timestamp in monotonic mode and null otherwise (default: null). |
Return:
string - Lowercase hex string with two characters per returned byte.
binary
Return the raw bytes produced by bytes().
public static function binary(
int $length = 10,
bool $monotonic = false,
?int &$timestamp = null
): stringParameters:
| Parameter | Type | Description |
|---|---|---|
$length | int | Number of source bytes (default: 10). |
$monotonic | bool | Whether to enable monotonic generation within the same process (default: false). |
$timestamp | int\|null | Output variable assigned the millisecond timestamp in monotonic mode and null otherwise (default: null). |
Return:
string - Raw binary string.
integer
Draw a PHP integer between inclusive bounds. Pass both bounds as decimal integer strings. The current implementation forwards them directly to BCMath, which rejects PHP integers under strict typing. This also affects the integer defaults. In monotonic mode, a later call in the same millisecond can increment the previous integer. Keep the bounds consistent for a monotonic sequence.
public static function integer(
string|int $min = self::UINT32_MIN,
string|int $max = self::UINT32_MAX,
bool $monotonic = false,
?int &$timestamp = null
): intParameters:
| Parameter | Type | Description |
|---|---|---|
$min | string\|int | Lower bound (default: self::UINT32_MIN). |
$max | string\|int | Upper bound (default: self::UINT32_MAX). |
$monotonic | bool | Whether to enable monotonic generation within the same process (default: false). |
$timestamp | int\|null | Output variable assigned the millisecond timestamp in monotonic mode and null otherwise (default: null). |
Return:
int - Generated integer.
Throws:
- Luminova\Exceptions\InvalidArgumentException if the bounds are nonnumeric or their order is invalid.
TypeErrorif either bound is passed as a PHP integer, including when using the defaults.OverflowExceptionif the previous monotonic value in the same millisecond has reached the upper bound.
bigInteger
Generate an integer string in the supplied inclusive range. When both bounds are omitted, the range is unsigned 64-bit. Explicit bounds should stay within a range of at most 2⁶⁴ values. Generation uses GMP when available or falls back to BCMath. Explicit upper bounds are validated using BCMath.
public static function bigInteger(?string $min = null, ?string $max = null): stringParameters:
| Parameter | Type | Description |
|---|---|---|
$min | string\|null | Lower bound. Omit it for zero (default: null). |
$max | string\|null | Upper bound. Omit it for the unsigned 64-bit maximum (default: null). |
Return:
string - Decimal integer string.
Throws:
- Luminova\Exceptions\InvalidArgumentException if an explicit upper bound triggers validation and the integer strings or their order are invalid.
unsignedBigInteger
Generate an integer in the unsigned 64-bit range. Requires BCMath for bound validation.
public static function unsignedBigInteger(): stringReturn:
string - Decimal integer string.
signedBigInteger
Generate an integer in the signed 64-bit range. Requires BCMath for bound validation.
public static function signedBigInteger(): stringReturn:
string - Decimal integer string.
generate
Generate a value using a named character set. An unrecognized type uses the default set. With TYPE_BYTES, the implementation requests half the length rounded up in source bytes. Use bytes() when an exact byte count matters.
public static function generate(
int $length = 10,
string $type = self::TYPE_INTEGER,
bool $uppercase = false
): stringParameters:
| Parameter | Type | Description |
|---|---|---|
$length | int | Requested output length (default: 10). |
$type | string | Output type selected by a TYPE_* constant (default: self::TYPE_INTEGER). |
$uppercase | bool | Whether to use uppercase letters for types that honor this option (default: false). |
Return:
string - Generated string, or an empty string for a length below one.
password
Draw characters from the password character set. Individual character classes can be absent from a generated value.
public static function password(int $length = 8): stringParameters:
| Parameter | Type | Description |
|---|---|---|
$length | int | Requested password length (default: 8). |
Return:
string - Generated string, or an empty string for a length below one.
ean
Generate a 13-digit EAN candidate with the selected prefix and a digit from calculateCheckDigit().
public static function ean(int $prefix = 615): stringParameters:
| Parameter | Type | Description |
|---|---|---|
$prefix | int | Numeric prefix of one to three digits (default: 615). |
Return:
string - EAN-13 digit string.
Throws:
- Luminova\Exceptions\InvalidArgumentException if the prefix is invalid.
upc
Generate a 12-digit UPC-A candidate with a digit from calculateCheckDigit().
public static function upc(int $prefix = 0): stringParameters:
| Parameter | Type | Description |
|---|---|---|
$prefix | int | Number-system digit from 0 to 9 (default: 0). |
Return:
string - UPC-A digit string.
Throws:
- Luminova\Exceptions\InvalidArgumentException if the prefix is invalid.
gmn
Generate a Global Model Number candidate with two check characters.
public static function gmn(int $length = 20): stringParameters:
| Parameter | Type | Description |
|---|---|---|
$length | int | Total length including check characters, from 3 to 25 (default: 20). |
Return:
string - Generated GMN string.
Throws:
- Luminova\Exceptions\InvalidArgumentException if the length is outside the supported range.
barcode
Generate a custom code using the uppercased prefix, random digits, and a final check digit.
public static function barcode(
string|int|null $prefix = null,
int $length = 12
): stringParameters:
| Parameter | Type | Description |
|---|---|---|
$prefix | string\|int\|null | Optional alphanumeric prefix (default: null). |
$length | int | Total length including prefix and check digit (default: 12). |
Return:
string - Generated barcode string.
Throws:
- Luminova\Exceptions\InvalidArgumentException if the prefix contains unsupported characters or the length leaves insufficient room.
calculateCheckDigit
Calculate a check value with alternating weights of 1 and 3, starting at 1 on the rightmost payload digit. Letters are mapped to values 10–35.
public static function calculateCheckDigit(string $code, int $modulo = 10): intParameters:
| Parameter | Type | Description |
|---|---|---|
$code | string | Nonempty alphanumeric code without its check digit. |
$modulo | int | Checksum modulus, at least two (default: 10). |
Return:
int - Check value from zero through modulus minus one.
Throws:
- Luminova\Exceptions\InvalidArgumentException if the code or modulus is invalid.
calculateGmnCheckCharacters
Calculate the two GS1 MOD 1021,32 check characters for GMN data.
public static function calculateGmnCheckCharacters(string $code): stringParameters:
| Parameter | Type | Description |
|---|---|---|
$code | string | GMN data of 1–23 characters without the check pair. |
Return:
string - Two-character check string.
Throws:
- Luminova\Exceptions\InvalidArgumentException if the length is unsupported or the input contains an unsupported character.