A Conversation for Use PHP to Get a Random Background Colour for a Website
Seeding
Nick Fel Started conversation Jun 2, 2003
Should the random number generator not be seeded to prevent a regular pattern of numbers being generated?
eg.
srand(microtime() * 1000000);
$number = rand(1,7);
That's what my PHP book says anyway
Seeding
OwlofDoom Posted Jun 2, 2003
Actually, I thought of that before Sub-editing this one, and actually did some testing...
I agree that my PHP4 book says it's necessary to seed the generator, as does the official documentation. However, doing a test myself with the scripts mentioned, I appeared to get a random-enough output. It certainly wasn't C-64 style 1,1,1,1,1,1,1,1,1,7,1,1,1 sort of random...
~
Seeding
R'win Posted Jun 2, 2003
Admitedly it relates to PHP4, but I found this in the official PHP documentation:
"In older versions of PHP, you had to seed the random number generator before use with srand(). Since 4.2.0 this is no longer necessary."
Also, in this context totaly random choices are not necesary since is is the change that is important (well, I think so anyway...)
Seeding
OwlofDoom Posted Jun 2, 2003
Yes, you are right. The pseudo-random number generators are designed to produce a good random-like sequence of numbers anyway...
Seeding
R'win Posted Jun 3, 2003
I've never bother with seeding as I've only been interested in getting different choices and they seem to be random enough.
How exactly does seeding make random choices more random?
Seeding
OwlofDoom Posted Jun 3, 2003
No computer can generate real random numbers without some kind of physical device (i believe there is such a thing made with mercury), so instead they just draw the numbers from a mathematical sequence with a long period, called a pseudo-random generator.
The seed is usually taken from the value of the computer's clock, and decides where in the sequence to start. I'm not sure exactly how PHP decides where to start without a seed, my guess is that it seeds the generator when the module first starts up and then stores its position in the sequence each time it executes, so it doesn't have to start from the seed each time.
~
Seeding
R'win Posted Jun 4, 2003
I see.
If seeding makes better random choices, why don't random functions always seed automatically?
Seeding
OwlofDoom Posted Jun 4, 2003
The seeding takes time, and pseudo-random number generators are non-sequential enough to not warrant seeding every time.
However, PHP should never be used for high-speed applications (in fact, some may say it's the slowest language in common use) and so seeding every time is not a problem - hence, I suppose, why they do reseed it every time a page is requested these days.
In languages like C, and those HLLs that wrap it, such as perl and python, one should not get any side effects from a routine unless the effect is specifically asked for! (Not quite true, as python implements garbage-collection, which is a very useful side-effect that most people don't mind happening behind their backs.)
~
Seeding
R'win Posted Jun 5, 2003
I've never really thought of PHP as being that slow, but then it is usually over the Internet so it's hard to tell...
Seeding
OwlofDoom Posted Jun 5, 2003
I might be able to find you a benchmark graph. It's embarrassingly slow, but then it's only designed to work with small programs of very low input/output...
Seeding
R'win Posted Jun 5, 2003
How slow, exactly? I assume we're talking minutes/seconds not micro-seconds?
Seeding
OwlofDoom Posted Jun 5, 2003
You don't measure programming language speeds in minutes and seconds, because it depends on how much input you're processing. All scripts take microseconds, usually - a _really_ complicated script might take over a second. Still slow, though.
Here's a benchmark, for example:
http://www.bagley.org/~doug/shootout/bench/sieve/detail.shtml
Here you can clearly see how slow PHP is compared with other languages, using a benchmark test called Sieve of Eratosthenes, which computes primes. At the bottom, you can see the number of seconds each language takes to compute N (a variable number of primes) ... gcc (ANSI C) is the fastest (which is no surprise) and java is the slowest (because of how long it takes to start the virtual machine) - you can see PHP is considerably slower than perl and python (0.26 seconds against 0.06 and 0.09 seconds respectively).
Seeding
OwlofDoom Posted Jun 6, 2003
The most worrying graph there, though, is the one for how much CPU each language requires to execute. You can see PHP as the green line shooting off upwards, only beaten by gawk (an old language, also only suitable for scripting) ... the fastest there is ghc, the Glasgow Haskell Compiler... I'm learning Haskell as we speak.
~
Seeding
OwlofDoom Posted Jun 6, 2003
It's a pure functional language, based on the maths it represents. For example, compare the following factorial functions in C and haskell...
int factorial (int n) {
int fac = 1;
for (int i=2; i<=n; i++)
fac *= i;
return fac;
}
factorial 1 = 1
factorial n = n * factorial (n-1)
Nice? I like it...
~
Seeding
Nick Fel Posted Jun 7, 2003
As someone said, it's over the net... since many people still use dial-up, they will still receive the data slower than it is processed.
My XMB message board's index page processes 23 queries in 0.3198249 seconds. It's really not meant for large applications, just message boards, guestbooks, news scripts and the like.
Key: Complain about this post
Seeding
- 1: Nick Fel (Jun 2, 2003)
- 2: OwlofDoom (Jun 2, 2003)
- 3: R'win (Jun 2, 2003)
- 4: OwlofDoom (Jun 2, 2003)
- 5: R'win (Jun 3, 2003)
- 6: OwlofDoom (Jun 3, 2003)
- 7: R'win (Jun 4, 2003)
- 8: OwlofDoom (Jun 4, 2003)
- 9: R'win (Jun 5, 2003)
- 10: OwlofDoom (Jun 5, 2003)
- 11: R'win (Jun 5, 2003)
- 12: OwlofDoom (Jun 5, 2003)
- 13: R'win (Jun 5, 2003)
- 14: OwlofDoom (Jun 6, 2003)
- 15: R'win (Jun 6, 2003)
- 16: OwlofDoom (Jun 6, 2003)
- 17: OwlofDoom (Jun 6, 2003)
- 18: Nick Fel (Jun 7, 2003)
More Conversations for Use PHP to Get a Random Background Colour for a Website
Write an Entry
"The Hitchhiker's Guide to the Galaxy is a wholly remarkable book. It has been compiled and recompiled many times and under many different editorships. It contains contributions from countless numbers of travellers and researchers."