Luminova Framework

PHP Luminova: Generate Random Values and Barcode Checksums

Last updated: 2026-11-28 20:57:12

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 bounds

Class 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'
): string

Parameters:

ParameterTypeDescription
$fromstringSource value to hash.
$lengthintRequested identifier length from 4 to 64 (default: 23).
$groupintGroup size. Use zero to skip grouping (default: 5).
$algostringHash algorithm (default: 'sha256').

Return:

string - Formatted identifier.

Throws:

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
): string

Parameters:

ParameterTypeDescription
$lengthintNumber of source bytes (default: 10).
$monotonicboolWhether to enable monotonic generation within the same process (default: false).
$timestampint\|nullOutput 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
): string

Parameters:

ParameterTypeDescription
$lengthintNumber of source bytes (default: 10).
$monotonicboolWhether to enable monotonic generation within the same process (default: false).
$timestampint\|nullOutput 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
): string

Parameters:

ParameterTypeDescription
$lengthintNumber of source bytes (default: 10).
$monotonicboolWhether to enable monotonic generation within the same process (default: false).
$timestampint\|nullOutput 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
): int

Parameters:

ParameterTypeDescription
$minstring\|intLower bound (default: self::UINT32_MIN).
$maxstring\|intUpper bound (default: self::UINT32_MAX).
$monotonicboolWhether to enable monotonic generation within the same process (default: false).
$timestampint\|nullOutput 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.
  • TypeError if either bound is passed as a PHP integer, including when using the defaults.
  • OverflowException if 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): string

Parameters:

ParameterTypeDescription
$minstring\|nullLower bound. Omit it for zero (default: null).
$maxstring\|nullUpper bound. Omit it for the unsigned 64-bit maximum (default: null).

Return:

string - Decimal integer string.

Throws:


unsignedBigInteger

Generate an integer in the unsigned 64-bit range. Requires BCMath for bound validation.

public static function unsignedBigInteger(): string

Return:

string - Decimal integer string.


signedBigInteger

Generate an integer in the signed 64-bit range. Requires BCMath for bound validation.

public static function signedBigInteger(): string

Return:

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
): string

Parameters:

ParameterTypeDescription
$lengthintRequested output length (default: 10).
$typestringOutput type selected by a TYPE_* constant (default: self::TYPE_INTEGER).
$uppercaseboolWhether 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): string

Parameters:

ParameterTypeDescription
$lengthintRequested 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): string

Parameters:

ParameterTypeDescription
$prefixintNumeric prefix of one to three digits (default: 615).

Return:

string - EAN-13 digit string.

Throws:


upc

Generate a 12-digit UPC-A candidate with a digit from calculateCheckDigit().

public static function upc(int $prefix = 0): string

Parameters:

ParameterTypeDescription
$prefixintNumber-system digit from 0 to 9 (default: 0).

Return:

string - UPC-A digit string.

Throws:


gmn

Generate a Global Model Number candidate with two check characters.

public static function gmn(int $length = 20): string

Parameters:

ParameterTypeDescription
$lengthintTotal length including check characters, from 3 to 25 (default: 20).

Return:

string - Generated GMN string.

Throws:


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
): string

Parameters:

ParameterTypeDescription
$prefixstring\|int\|nullOptional alphanumeric prefix (default: null).
$lengthintTotal length including prefix and check digit (default: 12).

Return:

string - Generated barcode string.

Throws:


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): int

Parameters:

ParameterTypeDescription
$codestringNonempty alphanumeric code without its check digit.
$modulointChecksum modulus, at least two (default: 10).

Return:

int - Check value from zero through modulus minus one.

Throws:


calculateGmnCheckCharacters

Calculate the two GS1 MOD 1021,32 check characters for GMN data.

public static function calculateGmnCheckCharacters(string $code): string

Parameters:

ParameterTypeDescription
$codestringGMN data of 1–23 characters without the check pair.

Return:

string - Two-character check string.

Throws: