All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To Generate Unique Random Number In PHP

Last Updated : Mar 11, 2024

How To Generate Unique Random Number In PHP

In this tutorial we will show you the solution of how to generate unique random number in PHP, here we defined an empty array then we inserting some range of numbers into an array and using shuffle() and print_r() methods we can easily generate unique numbers in php.

The shuffle() method used to randomizes the order of the elements in the array.

This function assigns new keys for the elements in the array. Existing keys will be removed.

Step By Step Guide On How To Generate Unique Random Number In PHP :-

Here we declared empty array and it’s name ‘$arr’. Using for loop we inserting integer values into array $arr up to range 15.

As we know shuffle method used to rearrange all elements and uniquely in array $arr then displayed all elements in array $arr using print_r() method.

After shuffle() method the original array $arr rearranged with new index and removed old indexes of all elements so result of array had updated elements with indexes.

<?php
$arr=array();
for($i=1;$i<15;$i++){
    $arr[]=$i;
}
shuffle($arr);
print_r($arr);
?>
  1. A php script can be placed anywhere in the document. A php script starts with <?php and end with ?>.
  2. The default file extension for php files is “.php” and php statements end with ‘;’ semicolon.
  3. Here we created empty array and named as $arr. As we know about for loop, this is used to iterate some amount of times particular process will be processed.
  4. So here we defined for loop up to range 15 so it will get iterated 14 times and also each time updating variable $i value we stored to our array variable ‘$arr’.
  5. For storing some numeric values into an array $arr then we passed this array as a parameter for shuffle() method.
  6. Shuffle() method will randomly organizing each values with key(index) also rearranged so our array $arr will totally gets updated like new array.
  7. Using print_r() method we displaying all values with index(key) of each elements in an array $arr on webpage.
  8. We can create different array with some other value initialization as your wish, here shown example for your reference only try to explore different values but concept will same so it won’t affect the result.
  9. We had two more methods also for generating unique random number those are rand(), mt_rand().

Conclusion :-

In conclusion we are able to know how to generate unique random number using php.

First we need to start our xampp server then we load this program on browser we can see result of array when we need to display any array with whole values we need to use print_r() method.

When we using above seen methods of rand() or mt_rand() we get result of random number but it won’t had guaranty of unique digits.

So we have to do some manual process to check whether unique or not and if had duplicate do process of elimination process also.

I hope this tutorial on how to generate unique random number in PHP helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Amruta

Amruta is an Experienced web developer with 4 years for experience she completed her master's with MCA and passionate about programming Languages for creating technical contents like HTML, CSS, JavaScript, Java, Python, PHP, jQuery.

Follow Amruta On Linkedin 🡪