SyntaxStudy
Sign Up
Home PHP Reference rand() / random_int()

rand() / random_int()

function

Generates a random integer. Use random_int() for cryptographically secure randomness.

Syntax

rand(int $min, int $max): int

Example

php
<?php
echo rand(1, 100);           // random 1-100
echo mt_rand(1, 6);          // dice roll

$colors = ['red','green','blue'];
echo $colors[array_rand($colors)];

// Cryptographically secure
$token = bin2hex(random_bytes(32)); // 64-char hex string