Hello,
How would I randomise a number between 0 and 100 and make it less likely to be a higher number than a lower. I.e so 5 would be more likely to be returned than 6.
Any one know how to do this?
Randomising a no. between 0 and 100.....
Moderator: General Moderators
-
DynamiteHost
- Forum Commoner
- Posts: 69
- Joined: Sat Aug 10, 2002 5:33 pm
Math Fun
It depends on how infrequent you want the higher numbers. I would use some function that will take your original random number and re-map it to a new number. You can then normalize the new number to fit back in your original 1-100 range.
One such function would be n*n, which is the same as "pow(n,2)":
Using the "pow()" function allows you to tweak the second argument to make it more skewed (higher argument value) or less skewed (lower argument value, but not less than 1, since "pow($floatNumber,1)" is the same as no remapping at all). A number less than 1 (but greater than 0!) would skew towards the higher numbers. Experimentation would be the key here.
This code is valid only if the mathematical operation in the "remap()" function takes a number between 0 and 1 and returns a number between 0 and 1. This is another reason "pow()" is a useful one to use.
One other thing to keep in mind with this is that $lowerbound and $upperbound are used in float functions, so a range of "1 to 100" really means "1.0 to 100.0" which is just a range of 99. "100.0" would rarely (or never) get returned depending on the function used in "remap()". You might want to use a $lowerbound value of 0 and a $upperbound value of 100. Then if the result is less than 100, add 1 to it.
One such function would be n*n, which is the same as "pow(n,2)":
Code: Select all
<?php
// define re-mapping function
function remap($floatNumber) {
return pow($floatNumber,2) ;
}
// get a random number between 0 and 1
$randval = mt_rand() / mt_getrandmax() ;
// apply the remapping function
$skewrandval = remap($randval) ;
// make the new number fit in the original range
$skewrandval = $skewrandval * ($upperbound - $lowerbound) + $lowerbound ;
// you can make the result an integer now
$finalrandval = floor($skewrandval) ;
?>This code is valid only if the mathematical operation in the "remap()" function takes a number between 0 and 1 and returns a number between 0 and 1. This is another reason "pow()" is a useful one to use.
One other thing to keep in mind with this is that $lowerbound and $upperbound are used in float functions, so a range of "1 to 100" really means "1.0 to 100.0" which is just a range of 99. "100.0" would rarely (or never) get returned depending on the function used in "remap()". You might want to use a $lowerbound value of 0 and a $upperbound value of 100. Then if the result is less than 100, add 1 to it.
-
DynamiteHost
- Forum Commoner
- Posts: 69
- Joined: Sat Aug 10, 2002 5:33 pm
Hopefully it won't be too difficult to implement this. I have a bad habit of being wordy, so I may have made it sound more complicated than it is. Try the code I included and assign values for $lowerbound and $upperbound and see what happens. All my other ramblings were the mathematician in me trying to be precise, but you can ignore them as you see fit. 