I want to generate a number that is close to zero with a certain general range. For example, let’s say I want the number to fall under 10 90% of the time, but there’s a small chance that it will be 15, 20, or even 30. The higher the number, the lower the chance of receiving it.
I’ve tried looking for something with the keywords “weighted probability” but can’t find anything that leads in the right direction.
Update:
I ended up using the Box-Muller transform (see accepted answer). Here’s the simple code I wrote:
const E = 2.71828183;
function getRandomCurvedValue(temp.median, temp.density) {
return this.getCurvedValue(random(0, 1), temp.median, temp.density);
}
function getCurvedValue(temp.value, temp.median, temp.density) {
return temp.median + (temp.density * log(E, (temp.value / (1 - temp.value))));
}
I think you’re looking for a normal distribution.
A normal distribution has two variables you can change: The mean and the standard deviation. The mean would be 0 in your case and the standard deviation appropriately chosen so that 90 percent of values fall below 10. (Based on the tables in the Wikipedia article, I think a standard deviation of 1.645 is appropriate.)
You can use the Box-Muller transform as an easy way of generating normal-distributed random values from uniformly-distributed ones. Also you would need to use the absolute value of the result since you only want values in the range of [0, ∞).