PHP generate random number using rand() / mt_rand() function

In this tutorial, we would love to share with you how to generate 2,4,6,10,12 digit unique random number in PHP? Let’s see how to generate random number in php

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.

Example of rand() function

<?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

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

<?php
 
echo(mt_rand() . "<br>"); // 
 
echo(mt_rand(10,99) . "<br>"); // generate 2 digit random number in php
 
echo(mt_rand(1000,9999) . "<br>"); // generate 4 digit random number in php
 
echo(mt_rand(100000,999999) . "<br>"); // generate 4 digit random number in php
 
echo(mt_rand(10000000,99999999) . "<br>"); // generate 8 digit random number in php

Note:

The PHP mt_rand() function generate a better random value. And also this function is 4 times faster than rand() function.

Recommended Posts:

7 Comments

  1. vurtilopmer said:

    I have not checked in here for some time because I thought it was getting boring, but the last few posts are great quality so I guess I?¦ll add you back to my daily bloglist. You deserve it my friend 🙂

    March 22, 2020
    Reply
  2. thefeed said:

    Excellent site you have here but I was wondering if you knew of any community forums that cover the same topics discussed in this article? I’d really like to be a part of group where I can get feed-back from other experienced people that share the same interest. If you have any suggestions, please let me know. Bless you!|
    thefeed http://breeze-courier.com/Redirect.asp?UID=16022739&SubSectionID=349&LinkID=227&LinkURL=https://www.polygon.com/users/skratchlabs

    December 24, 2020
    Reply
  3. the feed said:

    When I initially left a comment I appear to have clicked the -Notify me when new comments are added- checkbox and now each time a comment is added I recieve four emails with the same comment. Is there an easy method you can remove me from that service? Cheers!|
    the feed http://xsle.net/43ldg

    December 25, 2020
    Reply
  4. thefeed said:

    Hello I am so excited I found your blog, I really found you by accident, while I was looking on Digg for something else, Regardless I am here now and would just like to say cheers for a incredible post and a all round entertaining blog (I also love the theme/design), I don’t have time to look over it all at the moment but I have bookmarked it and also included your RSS feeds, so when I have time I will be back to read much more, Please do keep up the superb work.|
    thefeed http://syuriya.com/ys4/rank.cgi?mode=link&id=415&url=https://forum.svslearn.com/user/skratchlabs4

    December 26, 2020
    Reply
  5. solar powered atmospheric water generator said:

    My spouse and I absolutely love your blog and find nearly all of your post’s to be just what I’m looking for. Would you offer guest writers to write content for you? I wouldn’t mind writing a post or elaborating on many of the subjects you write regarding here. Again, awesome weblog!|
    solar powered atmospheric water generator http://ufa.region-pages.ru/findex/orgRedirect/17123?site=https%3A%2F%2Fwww.pexels.com%[email protected]

    January 6, 2021
    Reply

Leave a Reply

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