Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP rand(10000000000000,9999999999999) returning "-114888782"

Tags:

php

So, I have a PHP script:

<?
rand(1000000000000,9999999999999);

The expected result is a number with 13 digits.

But it's returning some weird numbers, as:

987419207
1032717476
-455563764

Does anyone know what's going on?


PHP: 5.2.17

OS: Tested on Debian Squeeze and Windows 7, both 64 bits


Solution (workaround)

<?
echo rand(10000,99999).rand(10000000,99999999);
like image 235
dmmd Avatar asked Dec 10 '12 14:12

dmmd


People also ask

What is the use of rand () function in PHP?

Definition and Usage 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 to give random number in PHP?

The rand() is an inbuilt function in PHP used to generate a random number ie., it can generate a random integer value in the range [min, max]. Syntax: rand(); The rand() function is used to generate a random integer.


2 Answers

Use getrandmax() to see the max value that you can get from rand(), its clearly a overflow problem.

you could use 2 of this int and make a longer one, calling rand for a 6 digit and again for a 7 digits, just an idea.

like image 92
Luis Tellez Avatar answered Oct 12 '22 22:10

Luis Tellez


i think 10000000000000 its not a valid integer!

output

getrandmax();
like image 22
silly Avatar answered Oct 12 '22 23:10

silly