2

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.

Ewan Heming
  • 4,628
  • 2
  • 21
  • 20
  • 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 Answers4

4

Fetch 8 random bytes from /dev/urandom or some equivalent source.

Denis de Bernardy
  • 75,850
  • 13
  • 131
  • 154
3

This?

rand(0, 18446744073709551615)

From the php documentation

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
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
1
$rand64bit = rand() << 32 | rand();
muto
  • 11
  • 1