5

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);
dmmd
  • 2,938
  • 4
  • 33
  • 41
  • 2
    Looks like overflow to me. – nickb Dec 10 '12 at 14:21
  • Did you check PHP_INT_MAX? Maybe for some reason you are exceeding int max, even though it should be larger on 64 bit – HashtagMarkus Dec 10 '12 at 14:22
  • Overflow plus the fact that 10000000000000 > 9999999999999 will not work, indeed. – Thomas Ruiz Dec 10 '12 at 14:24
  • My bad... it was 13 digits on both numbers. Edited – dmmd Dec 10 '12 at 14:27
  • The largest random number PHP can generate is 2147483647 (or 32767 on some system), so you can not generate 13-digit random numbers. But you can check this answer for a workaround http://stackoverflow.com/questions/1479823/in-php-how-do-i-generate-a-big-pseudo-random-number – Hoang Huynh Dec 10 '12 at 14:29
  • Take this link http://stackoverflow.com/questions/1587799/why-would-rand-return-a-negative-value-when-min-and-max-values-are-positive It will help you much in understanding. – anuj arora Dec 10 '12 at 14:38
  • Nice explanation, it's a bit clearer now. Thanks. – dmmd Dec 10 '12 at 14:40

3 Answers3

5

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.

Luis Tellez
  • 2,785
  • 1
  • 20
  • 28
  • 2147483647. How do I increase this? – dmmd Dec 10 '12 at 14:28
  • i dont belive you can, but 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. – Luis Tellez Dec 10 '12 at 14:30
  • 64-bit php on 64-bit hardware may help - it would certainly provide 64-bit integers (and that number is the max 32-bit signed integer value) [See here](http://stackoverflow.com/questions/864058/how-to-have-64-bit-integer-on-php) – Basic Dec 10 '12 at 14:32
  • What I have here is *supposed* to be a 64bit hardware. Go figure. Anyway... I just did rand().rand() with 4 digits in one and 9 in the other. It works. Thanks – dmmd Dec 10 '12 at 14:34
  • and still the other answer with my information is the + 5 and mine +1 haha. – Luis Tellez Dec 10 '12 at 14:35
  • The dot (concatenation operator) makes the result return as a string. – dmmd Dec 17 '12 at 16:08
4

i think 10000000000000 its not a valid integer!

output

getrandmax();
Mihai Matei
  • 24,166
  • 5
  • 32
  • 50
silly
  • 7,789
  • 2
  • 24
  • 37
0

Use a bignum library like BCMath or GMP. GMP is newer and seems to have a better API but that's just my opinion

Basic
  • 26,321
  • 24
  • 115
  • 201
  • 1
    I'm going with the workaround here... I don't need anything fancy for this atm. Thanks! – dmmd Dec 10 '12 at 14:37