6

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)

Tomasz Kowalczyk
  • 10,472
  • 6
  • 52
  • 68
user3490600
  • 255
  • 1
  • 3
  • 8
  • 2
    You are looking for a _random number generator_ that creates _the same numbers over and over again_ in a predictable way? Interesting. I suggest you create a sequence once and store and reuse it each time you need that sequence again. – arkascha Apr 06 '14 at 10:05
  • Unfortunately that wouldn't work, as I need it to generate solar systems whenever a new user creates their account. To make it simple, I'd like to store the seed in a database, then being able to render everything on screen dependent on that seed, with some form of PRNG. – user3490600 Apr 06 '14 at 10:09
  • @arkascha Yes, that's a very typical use of a *pseudo* random number generator. @user `mt_srand` + `mt_rand` doesn't work...? http://3v4l.org/AMWtr – deceze Apr 06 '14 at 10:11
  • 1
    @arkascha don't be a smart ass – Ben Apr 06 '14 at 10:12
  • As far as I've understood the way they work, is that you set the "mt_srand" or the "srand" and it will always generate the same number for you when calling either the "mt_rand" or the "rand". – user3490600 Apr 06 '14 at 10:13
  • @user Right. Doesn't it work for you? – deceze Apr 06 '14 at 10:14
  • I need a sequence of random numbers. – user3490600 Apr 06 '14 at 10:16
  • If you need a sequence then you call rand() or mt_rand() several times after seeding – Mark Baker Apr 06 '14 at 10:19
  • http://ideone.com/IoKixQ – Mark Baker Apr 06 '14 at 10:21

1 Answers1

8
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.

deceze
  • 510,633
  • 85
  • 743
  • 889
  • Updated with a little more explanation. – deceze Apr 06 '14 at 10:34
  • Just for the record, 64, 80,96 on my presumably completely different system and a good year and presumably php version later, too (So there are no (in this case) disturbing ‘salts’ in the implementation, it seems). – Frank N Oct 26 '15 at 11:51