mIRC Home    About    Download    Register    News    Help

Print Thread
#85105 03/06/04 02:10 PM
Joined: Sep 2003
Posts: 98
N
neznani Offline OP
Babel fish
OP Offline
Babel fish
N
Joined: Sep 2003
Posts: 98
I have a simple question smile
How does $rand (or any other random function) work ?


www.stwar.us.to
IRCnet - #stwar
#85106 03/06/04 02:53 PM
Joined: Dec 2002
Posts: 2,962
S
Hoopy frood
Offline
Hoopy frood
S
Joined: Dec 2002
Posts: 2,962
You mean how does it calculate a random number?

Short answer: It doesn't. Not a truly random number anyway. Random numbers generated by software actually use what's known as a pseudo-random number generator (PRNG), which is a complex algorithm that takes a number passed to it (known as a seed) and uses this as a starting number from which to operate the algorithm on. There are many different algorithms used for PRNGs, one of the better known ones is the Mersenne Twister.

Since all PRNGs are just forms of algorithms they are not truly random, and given the same seed they will yield the same number (this can actually be a very useful feature to take advantage of in certain programming situations). This also means that over a large enough period (a very very large period in modern computing) a PRNG will eventually repeat the exact same sequence of numbers.

If you're wondering why you don't have to pass a seed to $rand() when you use it it's because it's done automagically behind the scenes by mIRC.

See here for a more complete description of pseudo-random number generation.


Spelling mistakes, grammatical errors, and stupid comments are intentional.
#85107 04/06/04 12:08 PM
Joined: Sep 2003
Posts: 98
N
neznani Offline OP
Babel fish
OP Offline
Babel fish
N
Joined: Sep 2003
Posts: 98
Thank you smile


www.stwar.us.to
IRCnet - #stwar

Link Copied to Clipboard