rand

Need a fresh token, unpredictable number or unique-looking identifier? rand uses PHP’s cryptographic random source by default. With rand 1 100 you get one integer in that range; rand -n 5 1 10 prints five independently generated integers, which may repeat.

SecurityVersion 1.1.2Stable
lil-terminal

Overview

Use -b for cryptographically random bytes encoded as hex, -s for an alphanumeric string and -uuid for a version 4 UUID. A UUID is useful as an identifier, not as a secret. In the examples below the displayed values are samples; every normal invocation generates different output.

Random does not always mean secret

Secure integers, bytes and strings use the cryptographic random source. A UUID is designed as an identifier, not a secret, and -mt can be reproducible when seeded, so MT output must not be used for passwords, access tokens or cryptographic keys.

Syntax

lil-terminal
rand [min [max]]

Reference

rand

generate a cryptographic integer from 0 to PHP_INT_MAX

rand <max>

generate a cryptographic integer from 0 to max

rand <min> <max>

generate a cryptographic integer in the selected range

rand -n <count> [min] [max]

generate several integers

rand -b <bytes>

generate cryptographic random bytes as hexadecimal

rand -s <length>

generate a cryptographic alphanumeric string

rand -uuid

generate an RFC 4122 version 4 UUID

rand -mt [min] [max]

generate a fast non-cryptographic integer

rand -mt -seed <integer>

seed the MT generator before generation

rand -max

show integer generator limits

Notes

Minimum generates one integer with up to two bounds; -n, -s, -b, UUID and MT require Base.

Interactive sandbox preview

Run the prepared examples directly on the page. Their results are prebuilt and shown in the browser; no terminal command is executed on the server.

Generate a cryptographic integer from 0 to PHP_INT_MAX

Install and manage

Install the current compatible version. Choose Base for the complete feature set or Minimum for the reduced package; use upgrade to update a module that is already installed.

Base and Minimum are two packages of the same current module release, not separate command versions.

Base package

Install the complete package with local command reference and every published mode.

install rand
Minimum package

Install the reduced package. Local reference is omitted; help <cmd> -i opens the current server reference.

install rand -m
Exact public version

Install exactly this published version.

install rand -v 1.1.2
Check or update

Ask upgrade to check compatibility and replace the installed block only when needed.

upgrade rand
Remove module

Remove the extension block while leaving the core and unrelated modules intact.

install rand -u

Examples

Examples are copyable command lines, not actions executed by this website. Review paths, permissions and destructive flags before running them on a real project.

Generate a cryptographic integer from 0 to PHP_INT_MAX
rand
Sample output (a new value is generated on each run):
6717973524709714206
Generate a cryptographic integer from 0 to max
rand 100
Sample output (a new value is generated on each run):
75
Generate a cryptographic integer in the selected range
rand 10 20
Sample output (a new value is generated on each run):
13
Generate a cryptographic integer in the selected range
rand -5 5
Sample output (a new value is generated on each run):
5
Generate several integers
rand -n 5 1 10
Sample output (a new value is generated on each run):
7
3
10
6
4
Generate several integers
rand -n 3
Sample output (a new value is generated on each run):
191285834354115477
5305755011603380746
6738832702881509879
Generate cryptographic random bytes as hexadecimal
rand -b 16
Sample output (a new value is generated on each run):
439cce918df83a95d1d5ef3787e88e77
Generate cryptographic random bytes as hexadecimal
rand -b 32
Sample output (a new value is generated on each run):
2e31f17f5df14c264cf41cba1aa730a0405983ce0580f615302717c97d37acf4
Generate a cryptographic alphanumeric string
rand -s 24
Sample output (a new value is generated on each run):
RH0xmptLqFbZZ9cuyoxR07LN
Generate a cryptographic alphanumeric string
rand -s 32
Sample output (a new value is generated on each run):
pOyNEXu0WtVBRIyDBce329qQRBHj7RtC
Generate an RFC 4122 version 4 UUID
rand -uuid
Sample output (a new value is generated on each run):
6c8adb39-adb7-482a-aa3f-1e49570cff33
Generate a fast non-cryptographic integer
rand -mt 1 100
Sample output (a new value is generated on each run):
53
Seed the MT generator before generation
rand -mt -seed 42 1 100
Sample output (a new value is generated on each run):
43
Seed the MT generator before generation
rand -mt -seed 42 -n 3 1 10
Sample output (a new value is generated on each run):
3
8
7
Show integer generator limits
rand -max
secure:	9223372036854775807
mt:	2147483647
Generate a cryptographic integer in the selected range
rand 10 1
Minimum exceeds maximum
Generate several integers
rand -n 0
Count must be between 1 and 10000
Generate cryptographic random bytes as hexadecimal
rand -b 5000
Byte count must be between 1 and 4096
Generate a cryptographic alphanumeric string
rand -s 0
String length must be between 1 and 4096
Seed the MT generator before generation
rand -seed 42
-seed requires -mt
Generate cryptographic random bytes as hexadecimal
rand -b 16 -uuid
Choose one output mode
Generate a cryptographic integer in the selected range
rand 1 2 3
Too many arguments
Generate a cryptographic integer from 0 to PHP_INT_MAX
rand -mystery
Unknown option: -mystery.

Related documentation

Current public release

v1.1.22026-08-11

First stable release

Current stable release of rand. Need a fresh token, unpredictable number or unique-looking identifier?