<?php
$min_val = 3;
$max_val = 20;
$average_val = 5;
$loops = 0;
while ($loops < 100) {
$total += exprand($min_val, $max_val, $average_val);
$loops ++;
}
$average = $total/$loops;
echo '<br>';
echo gettype($total);
echo "<br> $total <br> $loops <br>";
echo $average;
//exponentially weighted random number generator
//generates random numbers throught the base formula
//f(x)=x^a where x is a random number and a is the weight
//function imputs are $min, $max, and $average
//function returns random float between $min and $max
//if run multiple times the average should be near $average
function exprand($min, $max, $average) {
//the origional exprand didn't accept $min
//the next 2 lines and the second $resault line add $min functionality
$max -=$min;
$average -=$min;
//calculating exponent
$exp = ($max/$average)-1;
//calculating max value for mt_rand
$maxrand = pow($max,(1/$exp));
//mt_rand returns integers
//rounding errors were causing major problems
//next 3 lines of code generate a random number with accuracy to the millionth
$maxrand *= 1000000;
$rand = mt_rand(0,$maxrand);
$rand /= 1000000;
//applying curve to $rand
$resault = pow($rand,$exp);
//add $min back in
$resault += $min;
return $resault;
}
?>