Ewan Heming
Ewan Heming

Reputation: 4648

PHP 64 bit Random Number

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.

Upvotes: 2

Views: 3836

Answers (4)

muto
muto

Reputation: 11

$rand64bit = rand() << 32 | rand();

Upvotes: 1

GolezTrol
GolezTrol

Reputation: 116110

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.

Upvotes: 3

Eric
Eric

Reputation: 97571

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.

Upvotes: 3

Denis de Bernardy
Denis de Bernardy

Reputation: 78443

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

Upvotes: 4

Related Questions