Generate unique random number in PHP. In this tutorial, we would love to share with you how to generate 2,4,6,10,12 digit unique random number in PHP.
How to Generate 4,6,8,10 digits Unique Random Number in PHP
You can use the php rand() and mt_rand() function to generate 2,4,6,10,12, etc digits unique random number in PHP. Let’s check out the code below for generating unique random numbers in PHP with 2, 4, 6, 10, 12, or any other specified number of digits:
PHP rand() function
The PHP rand() is inbuilt PHP function. Which is used to generate a random integer number.
Syntax
The basic syntax of rand() function is following:
rand(); or rand(min,max);
Parameters of rand() function
- min:- This a first parameter of this function and it is optional. Specifies the minimum/smallest/lowest number to be returned. Default is 0
- max:- This a second parameter of this function and it is also optional. Specifies the maximum/largest/highest number to be returned.
Here is an example code that generates unique random numbers of 4, 6, 8, and 10 digits in PHP:
// Function to generate unique random numbers
function generateUniqueRandomNumber($numDigits) {
$numbers = range(0, 9);
shuffle($numbers);
$randNumber = '';
for ($i = 0; $i < $numDigits; $i++) {
$randNumber .= $numbers[$i];
}
return $randNumber;
}
// Generate unique random numbers
$uniqueNumbers = array();
$uniqueNumbers[] = generateUniqueRandomNumber(4);
$uniqueNumbers[] = generateUniqueRandomNumber(6);
$uniqueNumbers[] = generateUniqueRandomNumber(8);
$uniqueNumbers[] = generateUniqueRandomNumber(10);
// Check for uniqueness and regenerate if needed
for ($i = 0; $i < count($uniqueNumbers); $i++) {
while (in_array($uniqueNumbers[$i], $uniqueNumbers)) {
$uniqueNumbers[$i] = generateUniqueRandomNumber($i == 0 ? 4 : ($i == 1 ? 6 : ($i == 2 ? 8 : 10)));
}
}
// Print the unique random numbers
print_r($uniqueNumbers);
PHP mt_rand() function
The PHP mt_rand () is the inbuilt PHP function. Which is used to generate random integer numbers. This function uses the Mersenne Twister algorithm.
Syntax
The basic syntax of mt_rand() function is the following:
mt_rand(); or mt_rand(min,max);
Parameters of mt_rand() function
- min:- This a first parameter of this function and it is optional. Specifies the minimum/smallest/lowest number to be returned. Default is 0
- max:- This a second parameter of this function and it is also optional. Specifies the maximum/largest/highest number to be returned.
Example of mt_rand() function
<!DOCTYPE html>
<html>
<body>
<?php
echo(rand() . "<br>");
echo(rand(10,99) . "<br>"); // generate 2 digit unique random number in php
echo(rand(1000,9999) . "<br>"); // generate 4 digit unique random number in php
echo(rand(100000,999999) . "<br>"); // generate 4 digit unique random number in php
echo(rand(10000000,99999999) . "<br>"); // generate 8 digit unique random number in php
?>
</body>
</html>
Note:
The PHP mt_rand() function generate a better random value. And also this function is 4 times faster than rand() function.
Recommended PHP Posts:
- Autocomplete Search Box in PHP MySQL
- Compare Arrays PHP | PHP array_diff() Function
- Get, Write, Read, Load, JSON File from Url PHP
- Functions: Remove First Character From String PHP
- Remove Specific/Special Characters From String In PHP
- How to Replace First and Last Character From String PHP
- Reverse String in PHP
- Array Push, POP PHP | PHP Array Tutorial
- PHP Search Multidimensional Array By key, value and return key
- json_encode()- Convert Array To JSON | Object To JSON PHP
- PHP remove duplicates from multidimensional array
- PHP Remove Duplicate Elements or Values from Array PHP
- Get Highest Value in Multidimensional Array PHP
- PHP Get Min or Minimum Value in Array
- Convert CSV to JSON PHP – PHP Tutorial
- PHP String to Uppercase, Lowercase & First Letter Uppercase
- PHP: isset() vs empty() vs is_null()
- Chunk_split PHP Function Example
- PHP Function: Date and Time With Examples
- Reverse Number in PHP | PHP Tutorial