2

I just got PHP VC11 x64 Thread Safe installed in Windows 8 64bit. I am trying to run random number by calling mt_rand(). As far as I know, mt_rand() use 32-bit MT algorithm and it should support [0, 4294967295]. But if I run mt_getrandmax(), it returns me 2147483647. So I think the number it return should be signed 32-bit integer.

I am trying to call mt_rand(-2147483648, mt_getrandmax()) 10 times, but most of the outcomes are zero. So I wonder if MT algorithm in this case doesn't return full 32-bit integers.

Amal Murali
  • 75,622
  • 18
  • 128
  • 150
user1285419
  • 2,183
  • 7
  • 48
  • 70
  • mt_rand returns an `int` according to the manual, which is a signed int by default. Its default minimum value is 0, but its maximum value is that of a signed int (and so is its minimum value). Your question would be improved if you could say what you actually try to achieve here. – Sanchises Jul 02 '14 at 13:21
  • I think this post might answer your question, http://stackoverflow.com/questions/5301034/how-to-generate-random-64-bit-value-as-decimal-string-in-php – Johnny Dew Jul 02 '14 at 13:25
  • thanks. I try to obtain a 32-bit random number. Based on Mersenne Twister algorithm, it supports 32-bit integer. I tested several different MT wrappers in C++/C/ and Ruby, they are all be able to return numbers in [0, 4294967295] for unsigned int or [-2147483648, 2147483647] for signed int. I want to know if the PHP version also support the same range or not. As you stated, the returned number is signed int and default min is 0 but it doesn't say anything on the actual lower bound. As my understanding, feeding -2147483648 as the min should be ok. But actual test doesn't support that. – user1285419 Jul 02 '14 at 13:32
  • Huh. codepad also has this issue: http://codepad.org/jsyv9o63 I'd imagine this is a version/platform bug since 3v4l doesn't show this: http://3v4l.org/F7dDo – Jim Jul 03 '14 at 08:08

0 Answers0