FrozenKing
FrozenKing

Reputation: 145

How do PHP and other languages pick random numbers?

Actually I was learning PHP, so this question just came into my mind. How does the computer pick a random number?

Upvotes: 4

Views: 159

Answers (3)

JvdBerg
JvdBerg

Reputation: 21856

Have a look at the wiki, its fairly good explained there.

Most computer random number are pseudo random. If you want really random numbers, you have to use white noise as a source, and digitize that.

Upvotes: 1

ProudOne
ProudOne

Reputation: 355

Depending on the programming / scripting language there are so called pseudo-random values. Because computers don't really have the option to just pick a number that comes to their minds (heh!), there's an algorithm / calculation that creates the number. The principle is very simple. The random value you get is only random because the way it is calculated is not known to you. If you had a randomizer function running for a while without changing it's seed (a value you can enter to change the calculation in the background) the values it gives would reoccur.

Upvotes: 1

Gigi
Gigi

Reputation: 29441

The random numbers we normally have access to from code are called pseudorandom numbers. They are based on mathematical sequences of numbers that only repeat themselves after a very, very long time. The place to start in the sequence is based on the seed, which is normally taken as a function of time.

The numbers appear to be random, but in reality they're not, and that's why they're called "pseudorandom".

Further reading: http://en.wikipedia.org/wiki/Random_number_generation

Upvotes: 2

Related Questions