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.
Asked
Active
Viewed 542 times
0

llanato
- 2,508
- 6
- 37
- 59

user3099705
- 27
- 4
-
Are you running a 32bit PHP or a 64-bit PHP? – Mark Baker Jan 21 '15 at 15:53
-
So what is it, `non-fixed/limitless` or `limited by mysql bigint range` ? ;-) – VolkerK Jan 21 '15 at 15:58
-
Maybe limited by mysql bigint range, maybe between 1 billion and 1 trillion, etc. – user3099705 Jan 21 '15 at 16:03
2 Answers
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