laws of averages

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

laws of averages

Post 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?
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
Todd_Z
Forum Regular
Posts: 708
Joined: Thu Nov 25, 2004 9:53 pm
Location: U Michigan

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
AKA Panama Jack
Forum Regular
Posts: 878
Joined: Mon Nov 14, 2005 4:21 pm

Post 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.
Post Reply