Floydian
09-01-2008, 08:02 PM
A lot of times, I see code that is designed to randomly choose an amount of money to give a user programmed like this:
$min = 100;
$max = 200;
$amount = rand($min, $max);
This pattern arises when you have min and max values stored in a database table like crimes perhaps. But this could apply to lots of things, even things that aren't money related.
The problem arises when the amounts get into the billion range. At about 2.1 billion, rand starts to spit out negative numbers. PHP's rand function is based on integers which is in contrast to say javascript or mysqls float based rand functions.
Because of this, that code pattern will break down with large numbers. How do we compensate?
function range_to_value($low, $high, $percent) {
$range = $high - $low;
$target = $range * $percent * 0.01 + $low;
return $target;
}
This is a nifty little function that I use that has saved me time programming many a script...
All it does is takes a min and max value, and then computes a value in the middle of that according to the percent passed into the function.
Instead of just figuring out the percent of the max value, the range begins at the min value and ends at the max value. Simply doing something like 200 * 0.5 (fifty percent of 200) results in 100, but suppose we want 50% in the middle of 100 and 200.
Well, the answer is 150, and the way to do it is:
function range_to_value($low, $high, $percent) {
$range = $high - $low;
$target = $range * $percent * 0.01 + $low;
return $target;
}
$min = 100;
$max = 200;
$amount = range_to_value($min, $max, rand(1,100));
Now, I've set that up to generate a random percent, but that third parameter could be set to 50.
Have fun with this! It will work with extremely large numbers. The only limitation is that the range, in this case a range of 100 numbers, is 100.
If your range is 1bill to 2 bill, the range would be 1bill, but with this code, you only get 100 variations. So be that as it may, you still get results spread out over that range, but there are only ever going to be 100 different results.
$min = 100;
$max = 200;
$amount = rand($min, $max);
This pattern arises when you have min and max values stored in a database table like crimes perhaps. But this could apply to lots of things, even things that aren't money related.
The problem arises when the amounts get into the billion range. At about 2.1 billion, rand starts to spit out negative numbers. PHP's rand function is based on integers which is in contrast to say javascript or mysqls float based rand functions.
Because of this, that code pattern will break down with large numbers. How do we compensate?
function range_to_value($low, $high, $percent) {
$range = $high - $low;
$target = $range * $percent * 0.01 + $low;
return $target;
}
This is a nifty little function that I use that has saved me time programming many a script...
All it does is takes a min and max value, and then computes a value in the middle of that according to the percent passed into the function.
Instead of just figuring out the percent of the max value, the range begins at the min value and ends at the max value. Simply doing something like 200 * 0.5 (fifty percent of 200) results in 100, but suppose we want 50% in the middle of 100 and 200.
Well, the answer is 150, and the way to do it is:
function range_to_value($low, $high, $percent) {
$range = $high - $low;
$target = $range * $percent * 0.01 + $low;
return $target;
}
$min = 100;
$max = 200;
$amount = range_to_value($min, $max, rand(1,100));
Now, I've set that up to generate a random percent, but that third parameter could be set to 50.
Have fun with this! It will work with extremely large numbers. The only limitation is that the range, in this case a range of 100 numbers, is 100.
If your range is 1bill to 2 bill, the range would be 1bill, but with this code, you only get 100 variations. So be that as it may, you still get results spread out over that range, but there are only ever going to be 100 different results.