On Fri, 19 Apr 2002, Gerard Samuel wrote:
> Im trying to get a random value to select a random row out of *a* 
> database.  Not just mysql.
> array_rand would do me just fine, but Im reading in the manual ->
> 
> "Don't forget to call *srand()* 
> <http://www.php.net/manual/en/function.srand.php> to seed the random 
> number generator. "
> 
> Im not too certain what it means to seed the generator, but is it 
> absolutely neccessary??
> The results Im looking for, doesn't have to be of any high precision.

Well, if you don't do it, you might get the same results each time. It's 
not very painful, so you might as well.

It's difficult for a computer to generate a number that's actually random,
because everything in the computer follows a deterministic process (how
would you write a program to pick a random number?). So there are
algorithms that will provide numbers in a non-repeating sequence (such as
the digits of pi) and these are branded as random number generators.
"Seeding" them basically means picking an arbitrary starting point in the
sequence. The typical way to do this is with the current time, since
that's at least not entirely predictable.

Some operating systems grab other bits of randomish data when they can and
use that to spice up the randomness of "random" numbers - they may take
the time interval between received network data or keyboard events or what
have you. Fancy setups used for cryptography (where randomness is very
important) even have physical devices attached that generate truly random
numbers (random based on our current understanding of physics) derived
from the observed decay of radioactive substances.

> Also I read on the srand man page that srand should only be called once.
> Does that mean one per page load or once every so often??

Once per page load is a good plan.

miguel


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to