Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating random integers within range with a probability distribution

I have a problem where I want to generate a set of random integer values between 1 and 5 inclusive using a probability distribution.

Poisson and Inverse Gamma are two distributions that show the characteristics I am after (majority at mean, less higher numbers) that I have found.

I am looking at using Apache Commons Math but I wasn't sure how to generate the numbers I wanted using the distributions available.

like image 907
jmc Avatar asked May 08 '13 08:05

jmc


People also ask

How do you generate a random number with a predefined probability distribution?

The basic idea goes like this: start from a random point x and take a random step xnew = x + delta. evaluate the desired probability distribution in the starting point p(x) and in the new one p(xnew) if the new point is more probable p(xnew)/p(x) >= 1 accept the move.

How do you generate random numbers from an arbitrary distribution?

If we want to generate a random sample according to a distribution F, we can generate a uniform random number on (0,1) and invert it by F. This is due to the fact that, if U is uniform on (0,1), then X=F−1(U) is a random variable that follows F.


1 Answers

From your problem description, it sounds like you actually want a sample generated from a discrete probability distribution, and you can use EnumeratedIntegerDistribution for this purpose. Choose appropriate probabilities for each of your integers, maybe something like the following would meet your needs:

int[] numsToGenerate           = new int[]    { 1,   2,    3,   4,    5   };
double[] discreteProbabilities = new double[] { 0.1, 0.25, 0.3, 0.25, 0.1 };

EnumeratedIntegerDistribution distribution = 
    new EnumeratedIntegerDistribution(numsToGenerate, discreteProbabilities);

int numSamples = 100;
int[] samples = distribution.sample(numSamples);

Just tweak the discreteProbabilities values to whatever you require.

like image 179
iainmcgin Avatar answered Sep 25 '22 14:09

iainmcgin