Jump to content

something better than rand or mt_rand


jonsjava

Recommended Posts

I got curious one day, so I wrote a small script that would run rand(1,1000) and mt_rand(1,000) 1,000,000 times each, and calculated the average. I had assumed that over time, the average would be somewhere around 500 (for every high, you would get a low). Well, the crazy thing is that it would hit 250 average every time, no matter the function (rand or mt_rand). I'm talking about within a decimal place of being exactly 250 for the average. That doesn't sound too random. So, the question: is there a class that is built for creating randoms that works better than the built-in random functions? If so, please point me to where I can find that.

 

Thanks.

Link to comment
Share on other sites

<?php
error_reporting(7);
$intCount = 1000000;
$intSum = 0;

for($i=0;$i<$intCount;$i++) {
    $intSum = $intSum + rand(1, 1000);
}
echo $intSum."--";
echo $intSum/$intCount;
?>

 

this code gives me average as 500.something...as expected...same with mt_rand()

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.