Generating random numbers in PHP... I need more variations?

  • Thread starter Thread starter Duaa Momani
  • Start date Start date
D

Duaa Momani

Guest
Hi,
I am using the PHP random function (rand(start,end)) but I have noticed that whatever number of times I run the function, it gives me about 25% of randoms generated in the first quarter of range, and 25% of the second one and so on...

e.g. if I used rand(1,4) and ran this function 10000 times or even 100000000 times then I got the following percentages:
Number of 1's generated = almost 25% of all numbers generated
number of 2's = about 25%
number of 3's = about 25%
number of 4's = about 25%

My question is: how can I get more variations and not getting these very close percentages?
I tried generating like rand(0,1000) or rand(0,1000000000) and tried a lot of small or large numbers but I still getting the same problem...
 
<?php
// seed with microseconds
function make_seed()
{
list($usec, $sec) = explode(' ', microtime());
return (float) $sec + ((float) $usec * 100000);
}
mt_srand(make_seed());
$randval = mt_rand();
?>
 
You realize percentages are just a number game.. the percentages will always be the same (if enough trials are given)..Meaning.. If you flip a coin enough times, it will ALWAYS be around equal..

What you can try to do is create it so it has to run through multiple random numbers. I don't think that's going to make sense so let me try and explain it (in not code form)

Random number 1 (is between 1-10,000)
Random number 2 (is between 1-10,000)
Random number 3 (is between 1-10,000)

Then add them together.. then divide it or something.. Try to use some of the PHP math functions..

But even with all of this, laws of statics won't change. There will be a percentage on this one too, it just might take a lot of time but there will be a percentage it equals to.
 
Back
Top