HiveBrain v1.2.0
Get Started
← Back to all entries
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)

Submitted by: @import:stackexchange-cs··
0
Viewed 0 times
randomclosenonaveragelikeuniform100getbetweenthat

Problem

Presume I have the following case:

  • int value a



  • int value b, for which a



  • int value i, for which a



  • I need an int value x, for which a



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 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.