Drummin Posted August 8, 2012 Share Posted August 8, 2012 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"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/266828-true-percent-average-over-thirty-days/ Share on other sites More sharing options...
ManiacDan Posted August 8, 2012 Share Posted August 8, 2012 So you want to generate random numbers that always add up to the same value? That's not how randomness works. Quote Link to comment https://forums.phpfreaks.com/topic/266828-true-percent-average-over-thirty-days/#findComment-1367926 Share on other sites More sharing options...
Drummin Posted August 8, 2012 Author Share Posted August 8, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/266828-true-percent-average-over-thirty-days/#findComment-1367928 Share on other sites More sharing options...
requinix Posted August 8, 2012 Share Posted August 8, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/266828-true-percent-average-over-thirty-days/#findComment-1367931 Share on other sites More sharing options...
Drummin Posted August 8, 2012 Author Share Posted August 8, 2012 Thank you both for your help. Quote Link to comment https://forums.phpfreaks.com/topic/266828-true-percent-average-over-thirty-days/#findComment-1367932 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.