Generate 4,6,8,10 digits Unique Random Number in PHP

Generate 4,6,8,10 digits Unique Random Number in PHP

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:

  1. Autocomplete Search Box in PHP MySQL
  2. Compare Arrays PHP | PHP array_diff() Function
  3. Get, Write, Read, Load, JSON File from Url PHP
  4. Functions: Remove First Character From String PHP
  5. Remove Specific/Special Characters From String In PHP
  6. How to Replace First and Last Character From String PHP
  7. Reverse String in PHP
  8. Array Push, POP PHP | PHP Array Tutorial
  9. PHP Search Multidimensional Array By key, value and return key
  10. json_encode()- Convert Array To JSON | Object To JSON PHP
  11. PHP remove duplicates from multidimensional array
  12. PHP Remove Duplicate Elements or Values from Array PHP
  13. Get Highest Value in Multidimensional Array PHP
  14. PHP Get Min or Minimum Value in Array
  15. Convert CSV to JSON PHP – PHP Tutorial
  16. PHP String to Uppercase, Lowercase & First Letter Uppercase
  17. PHP: isset() vs empty() vs is_null()
  18. Chunk_split PHP Function Example
  19. PHP Function: Date and Time With Examples
  20. Reverse Number in PHP | PHP Tutorial

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

Your email address will not be published. Required fields are marked *