All TalkersCode Topics

Follow TalkersCode On Social Media

Generate Random Number In PHP Without Repetition

Last Updated : Jan 1, 2023

Generate Random Number In PHP Without Repetition

In this tutorial we will show you the solution of generate random number in PHP without repetition, mainly there are many ways with help of which we are able to generate random numbers. This concept is mainly used in OTP system.

For example, in some website we want to reset a password and want to confirm email id and password. So, we use this method to validate.

Step By Step Guide On Generate Random Number In PHP Without Repetition :-

Here, we already said that there are many ways. So, let us understand with most efficient way that is by creating a function and using for loop.

Here below we are going to show you code in which we understand this method properly.

<!DOCTYPE html>
          <title> php generate random number without repetition </title>
function randomNumber($n) {
 $characters = '0123456789';
 $randomString = '';
 for ($i = 0; $i < $n; $i++) {
  $index = rand(0, strlen($characters) - 1);
  $randomString .= $characters[$index];
 return $randomString;
echo randomNumber($n);
  1. First, we write <! DOCTYPE html> which we used as an instruction to the web browser about what version of HTML file is written in.
  2. Secondly, the <html> tag is used to indicate the beginning of an HTML document.
  3. As above now <head> tag is used to contain information about web page. In this tag a <title> tag is used which helps us to specify a webpage title. Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
  4. Here, then we create a body tag. All the content which we want to show on browser’s screen or display is always written inside this codes.
  5. Here, as we can see that we create our basic php structure. And inside this structure as we see that we give a limit. This is because we want to show number of fixed length. And we generate here number of 6.
  6. After that we create a function and pass a parameter which we call later. Inside this function we store our all characters in $characters variable. In this case all characters are characters which we want to show.
  7. Now, we use for loop to generate random string and inside this rand function is used. As we know rand is used to generate a random number. We use this rand on our specified characters and after that we store them in a variable that is randomString.
  8. At last we return this randomString and call function at end of function. you can see reference in above code.
  9. At last, the <body> and <html> tags are closed with </body> and </html> respectively.

Conclusion :-

At last in conclusion, here we can say that with the help of this article we are able to understand how we are able to generate random string of fixed length using php.

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

Latest Tutorials