random_int(): Random integer generator
echo random_int(8,10); // Random Output starting from 8 to 10
Syntax
random_int ( min,max);
min ,max : Minimum and maximum value ( both inclusive ) of random integer to be generated ( both to be given )
Available in PHP 7, PHP 8
We can generate random value by using rand() PHP. We can specify a range and the random value between those two ranges (inclusive) will be generated.
Can be used for cryptographic random integers for shuffling a deck of cards or poker games.
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: Generating Lottery Numbers
<?php
for ($i = 0; $i < 6; $i++) {
echo random_int(1, 49) . " "; // Output: 6 random lottery numbers between 1 and 49
}
?>
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
random() →
Rounding off value →
← Subscribe to our YouTube Channel here
This article is written by plus2net.com team.
https://www.plus2net.com
plus2net.com