Page 1 of 1

laws of averages

Posted: Sat Apr 01, 2006 9:00 pm
by s.dot

Code: Select all

$rand = rand(1,5);
if($rand == 2){
    //   do some stuff
}
this is a code im using for an april fools day joke ( i load an error page - really smurfs people off :lol: ) however it seems to load like 1 out of 30 pages loads.

are the results cached or something?

I realize mt_rand() is 4 times faster, but does it mean that rand() isn't as random?

Posted: Sat Apr 01, 2006 9:05 pm
by Todd_Z
This of course is based on no testing, but I think this is a case of red-car-syndrome. You look out for something to happen, it happens a whole lot more often than it should. Run a test, keeping track of the values of the rand() call.

Posted: Sat Apr 01, 2006 9:05 pm
by feyd
rand() and mt_rand() are both pseudo-random functions. They are math progressions that eventually wrap given enough calls. mt_rand has a MUCH longer span than rand, but it will eventually start repeating.

Sadly, there are few real random sources available (although technically, most CPU's has a few sources for ~truely random data -- although not labeled as such).

Overall, random in a computer is anything but random.

Posted: Sat Apr 01, 2006 9:06 pm
by s.dot
indeed :-D

not worthy of testing, as it was my 20 second script to try to annoy people as much as possible with as little effort as possible :-D

however, just refreshing pages I should get the desired result one out of every 5 page loads. its a far cry from that currently

Posted: Sat Apr 01, 2006 9:19 pm
by AKA Panama Jack
Use this...

Code: Select all

mt_srand(hexdec(substr(md5(microtime()), -8)) & 0x7fffffff);  // create a new random seed based off the system clock

$rand = mt_rand(1,5); 
if($rand == 2){ 
    //   do some stuff 
}

You will find the randomization is far more random. :) If you do not use a seed value the random function will use the same seed point and things will not be that random.