does anyone know a simple way to generate a random 64 bit number using PHP? It would be best if it doesn't rely on any extensions if possible.
Asked
Active
Viewed 3,563 times
2
-
Maybe this link can help - http://stackoverflow.com/questions/4523343/how-do-you-generate-a-random-number-over-the-32bit-limit-in-php – lethalMango Jun 08 '11 at 21:34
4 Answers
4
Fetch 8 random bytes from /dev/urandom or some equivalent source.

Denis de Bernardy
- 75,850
- 13
- 131
- 154
-
-
1It's a file... `$fp = fopen('/dev/urandom'); fread($fp, 8); fclose($fp);` – Denis de Bernardy Jun 08 '11 at 22:09
-
1
3
This?
rand(0, 18446744073709551615)
On some platforms (such as Windows), getrandmax() is only 32768. If you require a range larger than 32768, specifying min and max will allow you to create a range larger than this, or consider using mt_rand() instead.

Eric
- 95,302
- 53
- 242
- 374
-
This works for `mt_rand` too, but it only returns values different from 0 for smaller numbers than the one given above. – likeitlikeit Mar 27 '14 at 11:52
-
1This gives "Warning: rand() expects parameter 2 to be integer, float given in - on line 1" on PHP 7. – Janus Troelsen Jul 24 '15 at 18:15
-
Also, the results may not be distributed evenly: https://bugs.php.net/bug.php?id=63174#1437000589 – Janus Troelsen Jul 24 '15 at 18:16
3
Generate a bunch of smaller (16 bits) random numbers using mt_rand and combine them. This approach is also used in this UUID example on php.net.

GolezTrol
- 114,394
- 18
- 182
- 210
-
That seemed to work. Although, when I converted it using `base_convert` it was too large? I think it might be a problem with the precision of that function? – Ewan Heming Jun 10 '11 at 07:21