Jump to content

Recommended Posts

Is there a way to create a random daily percent, + or - some value that will accurately return a target percent average for a given month.

In this code I have a target goal of 15 and a plus or minus value of 4.  I'm using rand() to create value for the day.  However when running and tallying the result I get anywhere from 13.7 to 16.3 for example.  Is there any way to do this and get a true average that matches the goal amount?

<?php
$goal=15;
$c=4;
$rhigh=$goal+$c;
$rlow=$goal-$c;
echo "$goal<br />";
$total="";

for($i=1; $i<=30; $i++){
$percent=rand($rlow,$rhigh);
echo "Day $i Percent: $percent<br />";
$total +=$percent;
}

$result=$total/30;
$result=number_format($result, 1, '.', '');	

echo "$result";
?>

Link to comment
https://forums.phpfreaks.com/topic/266828-true-percent-average-over-thirty-days/
Share on other sites

This is for a simulator where you set a target percent to run numbers through.  As mentioned the plus or minus number will also be set in a final application.  I'm looking for a way to accurately represent daily averages within these parameters and the average result over thirty days is the target percent. 

You have two choices:

1. Randomness and know that given a large enough sample size you will see results trend to 15. With small sample sizes it will be close but could vary wildly (with larger variations being exponentially unlikely).

2. Not randomness but rotation. First visitor is A=11-15, second visitor is 30-A, third visitor is C=11-15, fourth visitor is 30-C, and so on.

 

Do not try to mix the two together. Either it's randomness and you accept that it will not always be exactly 15, or it's rotation and you get your ideal split.

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.