Php random number 10 digits

Php random number 10 digits

rand() or mt_rand() functions can be used to Generate 10 Digit Random Number in PHP. Examples given below show how to Generate 10 Digit number using rand and mt_rand functions.

Generate 10 Digit Random Number using rand()

To Generate 10 Digit number using rand() function, we will define the lowest number and highest number as it's parameters. The rand() function will return a random integer between min and max numbers (including min and max numbers).

Syntax of rand()

The syntax of rand() function is given below.

rand(min,max);
?>

Example 1


Output

2387509223

Generate 10 Digit Random Number using mt_rand()

mt_rand() function is just like rand() function but it is 4 times faster than rand() function. To use mt_rand() function, define min and max numbers. The mt_rand() function will return a random integer between min and max numbers (including min and max numbers).

Syntax of mt_rand()

The syntax of mt_rand() function is given below.

mt_rand(min,max);
?>

The mt_rand() returns false if second number is smaller than first one.

Example 2


Output

7128830561

Also Read:

Generate 1 Digit Random Number in PHP
Generate 2 Digit Random Number in PHP
Generate 3 Digit Random Number in PHP
Generate 4 Digit Random Number in PHP
Generate 5 Digit Random Number in PHP
Generate 6 Digit Random Number in PHP
Generate 7 Digit Random Number in PHP
Generate 8 Digit Random Number in PHP
Generate 9 Digit Random Number in PHP


I am trying to generate a random 10 digit number and insert this into mysql on page load, so each time the page refreshes or loads there should be a different 10 digit number being inserted into the database, however at the moment it just keeps inserting the same 10 digit number again and again.

Can someone please show me where I am going wrong? Thanks.


This is the number I am constantly getting: 2147483647

asked Apr 29, 2014 at 11:07

4

try to getting ten digit rand no

$randnum = rand(1111111111,9999999999);

answered Apr 29, 2014 at 11:11

Php random number 10 digits

Rakesh SharmaRakesh Sharma

13.6k4 gold badges36 silver badges42 bronze badges

3

You need to change column type. The number that you try to insert is higher than column maximum value (in your case SIGNED INT - 2147483647)

SIGNED INT - 2147483647
UNSIGNED INT - 4294967295

SIGNED BIGINT - 9223372036854775807
UNSIGNED BIGINT - 18446744073709551615

answered Apr 29, 2014 at 11:21

You're getting 2147483647 because it's the highest signed 32 bit integer that exists (binary it's 1111111111111111111111111111111). This is also the contents of the constant PHP_INT_MAX.

Just use a string with a loop and the mt_rand() function:

DEMO

answered Apr 29, 2014 at 11:16

h2oooooooh2ooooooo

38.2k8 gold badges65 silver badges101 bronze badges

You can also try this if you want. Its not generating a random number but a random string

private string generaterandomnumber()
        {
            string Rand1 = RandomString(8);
            string Rand2 = RandomString(8);
            docNum = Rand1 + "-" + Rand2;
            MD5 md5 = new MD5CryptoServiceProvider();
            md5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(docNum));

            //get hash result after compute it
            byte[] result = md5.Hash;

            StringBuilder strBuilder = new StringBuilder();
            for (int i = 0; i < result.Length; i++)
            {
                //change it into 2 hexadecimal digits
                //for each byte
                strBuilder.Append(result[i].ToString("x2"));
            }
            return strBuilder.ToString();
        }

Downvoting not required!!! Thx

answered Apr 29, 2014 at 11:31

ProfessorProfessor

6094 silver badges20 bronze badges

You should consider using the mt_rand function as gives a better distribution range.

In your code, you are not using the full 10 digit number range, try something like

$MIN_SESSION_ID = 1000000000;
$MAX_SESSION_ID = 9999999999;

$randId = mt_rand($MIN_SESSION_ID, $MAX_SESSION_ID);

echo $randId;

answered Apr 29, 2014 at 11:17

KamiKami

18.8k4 gold badges49 silver badges63 bronze badges

Use this code.

$x = 10; // Amount of digits
$min = pow(10,$x);
$max = pow(10,$x+1)-1);
$value = rand($min, $max);

answered Apr 29, 2014 at 11:09

Php random number 10 digits

1

Use php mt_rand() function instead of rand()


answered Apr 29, 2014 at 11:13

dkakotidkakoti

6028 silver badges25 bronze badges

function random_num ($len)
{
 $ch = "0123456789";
 $l = strlen ($ch) - 1;
 $str = "";
 for ($i=0; $i < $len; $i++)
 {
     $x = rand (0, $l);
     $str .= $ch[$x];
 }
 return $str;
}

answered Apr 29, 2014 at 11:14

There is no problem with your number generating code. Try unsetting your variables before they are defined and see if it fixes the problem.

unset($num0, $num1, $num2, $num3);
$num0 = (rand(10,100));
$num1 = date("Ymd");
$num2 = (rand(100,1000));
$num3 = time();
$randnum = $num0 . $num1 . $num2 . $num3;

You could just as easily use the rand function to generate a 10 digit number by using the following code: rand(1111111111,9999999999)

answered Apr 29, 2014 at 11:19

Php random number 10 digits

When I run your script I get different numbers everytime. Why don't you use the uniqid() function ?

Php random number 10 digits

Reporter

3,7965 gold badges31 silver badges46 bronze badges

answered Apr 29, 2014 at 11:16

0

Here is the simple way of generating 10 digit random number


answered Jul 25, 2015 at 11:56

Php random number 10 digits

It is the most suitable method to remember that when you did generate this key/number

echo $key = time(); 

its 10 digit number that convert this into exact date and time.

answered Jan 5 at 10:39

Php random number 10 digits

Not the answer you're looking for? Browse other questions tagged php mysql or ask your own question.

How do I generate a random 10 digit number in PHP?

The rand() function generates a random integer. Example tip: If you want a random integer between 10 and 100 (inclusive), use rand (10,100). Tip: As of PHP 7.1, the rand() function has been an alias of the mt_rand() function.

How do you generate a random 10 digit number in Python?

import random n = random. random() print(n).
import random n = random. randint(0,22) print(n).
import random randomlist = [] for i in range(0,5): n = random. randint(1,30) randomlist. ... .
import random #Generate 5 random numbers between 10 and 30 randomlist = random. sample(range(10, 30), 5) print(randomlist).

What is Mt_rand function in PHP?

The mt_rand() function is a drop-in replacement for the older rand(). It uses a random number generator with known characteristics using the » Mersenne Twister, which will produce random numbers four times faster than what the average libc rand() provides.

How do I generate a random 10 digit number in C#?

Would nine zeros followed by a 1 be valid for you? ... .
Thanks, I found this a helpful reference. ... .
Random random = new Random(); StringBuilder sb = new StringBuilder(); for (int i = 0; i < 10; i++) { sb.Append(random.Next(0, 9).ToString()); } return sb.ToString(); ... .
1) What are you using this number for?.