Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - Pseudo Random Number Generator?

Over the past couple of days, I've been trying to find a good way to generate random numbers in PHP, based on a seed. Like I'm sure most of you already know, the php rand() method is way too random for some cases, and I really need a PRNG that lets me generate the same sequence numbers over and over again based on a seed.

I've already attempted using the XORShift PRNG, the problem comes as different operating systems seem to generate different answers beause of how PHP handles Bit-shifting.

I would need some sort of algorithm that works well with PHP, that is able to generate quite large numbers, as I'll put a zero in front of it anyway and turn it into a small double. (0.RAND)

like image 990
user3490600 Avatar asked Apr 06 '14 10:04

user3490600


People also ask

How do I generate a random number in PHP?

The rand() function generates a random integer. Example tip: If you want a random integer between 10 and 100 (inclusive), use rand (10,100). Tip: As of PHP 7.1, the rand() function has been an alias of the mt_rand() function.

How do I generate a random 4 digit number in PHP?

RAND()*(9999-1000)+1000: This will mainly multiply 9999 with the RAND function to generate 4-digit numbers. INT(RAND()*(9999-1000)+1000: This will take the closest integer of the random number and generate only the 4-digit on random numbers.

How do I generate a random 10 digit number in PHP?

rand() or mt_rand() functions can be used to Generate 10 Digit Random Number in PHP.

What is the difference between Rand and Mt_rand in PHP?

The mt_rand() function is a drop-in replacement for the older rand(). It uses a random number generator with known characteristics using the » Mersenne Twister, which will produce random numbers four times faster than what the average libc rand() provides.


1 Answers

mt_srand(42);

echo mt_rand(1, 100);
echo mt_rand(1, 100);
echo mt_rand(1, 100);

This produces the sequence 64, 80, 96 on my system. It will do so every time you execute this code. You seed the generator once with your specific number (here 42), then call the generator again and again to produce a random number.


A random number generator (truly unpredictable randomness) cannot be seeded and produces a truly unpredictable random number. Computers typically cannot do this, since randomness is precisely what they do not do. Computers are deterministic and cannot produce true randomness. You need to do something like measuring radioactive decay to produce true randomness.

Pseudo random number generators appear on the face of it to behave randomly, but they are not. They start with one number, then apply deterministic mathematical operations to that number to change it and produce a different number. Each time you call the generator, it will produce a new number based on its last number. The sequence of a PRNG is therefore always the same. Good PRNGs apply operations in such a fashion that the sequence looks very randomly distributed. Typically they're seeded with something like the time of day, so they appear random if you don't seed them explicitly. If you seed them with a specific value though, they'll produce a fixed predetermined sequence of numbers.

like image 198
deceze Avatar answered Sep 18 '22 08:09

deceze