snippetMinor
Non-uniform random distribution: How do I get a random between 100 and 180 that is on average close to 120? (like in a Gaussian distribution)
Viewed 0 times
randomclosenonaveragelikeuniform100getbetweenthat
Problem
Presume I have the following case:
In Java, I have the following methods available on java.util.Random
- int value
a
- int value
b, for whicha
- int value i, for which a
- I need an int value
x, for whicha
In Java, I have the following methods available on java.util.Random
:
nextInt(int m): int between 0 and m
nextDouble(): double between 0.0 and 1.0
nextGaussian(): double between -Infinity and +Infinity, usually close to 0.0.
How do I build such a non-uniform distribution from these building blocks? How do I reliably and efficiently transform nextGaussian() into nextGaussian(a, b, i)`? The part I am struggling with is to enforce that x is selected between a and b (without doing a trial-and-error).Solution
Your problem is rather vague: "bell curve" and "centered around i" are not well defined.
One possible recipe is to generate a truncated gaussian: define
Related.
One possible recipe is to generate a truncated gaussian: define
s= min(b-i,i-a)/c where c is a free parameter (suggested values: [1,3] range), generate a gaussian with mean i and standard deviation s - and round it to the nearest integer. If the value falls outside the [a,b] range, try again.Related.
Context
StackExchange Computer Science Q#18467, answer score: 5
Revisions (0)
No revisions yet.