random_int(): Random integer generator

Generate a cryptographically secure random integer

random_int() returns a uniformly selected integer in the inclusive range you provide and is suitable for security-sensitive uses that need an unpredictable integer.

$code = random_int(100000, 999999);
echo $code;
echo random_int(8,10); // Random Output starting from 8 to 10 
Syntax
int random_int(int $min, int $max)
min ,max : Minimum and maximum value ( both inclusive ) of random integer to be generated ( both to be given )

random_int() is available in PHP 7 and PHP 8. It returns a uniformly selected integer from the inclusive range and is suitable when the result must be unpredictable.

Use random_int() for security-sensitive integer selection. For shuffling collections, the newer Random\Randomizer API provides dedicated shuffle methods.
Note that here we are generating a random number only. To get any random characters we can use arrays. The details on how to generate random characters is available here.

Example 1: Generating Random Numbers Between 1 and 100

<?php
echo random_int(1, 100);  // Output: A random number between 1 and 100
?>

Example 2: Random Number for Cryptographic Purposes

<?php
echo random_int(1000, 9999);  // Secure random number between 1000 and 9999
?>

Example 3: Six random draws (duplicates possible)

<?php
for ($i = 0; $i < 6; $i++) {
    echo random_int(1, 49) . " ";  // Six independent draws between 1 and 49; duplicates are possible
}
?>

Example 4: Generating Random Negative Integers

<?php
echo random_int(-50, -1);  // Output: A random negative number between -50 and -1
?>

How to generate random string having alphabets and digits
Rounding off value
Math Functions rand(): non-secure pseudo-random integers Rounding off value


Subscribe to our YouTube Channel here



plus2net.com











PHP video Tutorials
✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer