0

How can I generate a non-fixed length large random number? (e.g. within the range of MySQL's bigint). The mt_rand() function does not support such large numbers.

llanato
  • 2,508
  • 6
  • 37
  • 59

2 Answers2

0

Use multiple random functions:

const max_limit=10000;
a=mt_rand()*max_limit*max_limit + mt_rand()*max_limit + mt_rand();
Arashium
  • 325
  • 2
  • 9
0

MySQL's BIGINT is 8 bytes large. Let's asume an unsigned BIGINT.

echo unpack('H*', mcrypt_create_iv(8))[1];

or for php versions < 5.4 that do not support function array dereferencing

echo current(unpack('H*', mcrypt_create_iv(8)));

see:
http://docs.php.net/function.mcrypt-create-iv
http://docs.php.net/function.pack

VolkerK
  • 95,432
  • 20
  • 163
  • 226