javarandomexponential-distribution

Generating random numbers in java such that they average to a particular value


I am thinking of using an exponential distribution for this random number generator and If i am generating about a 100 numbers, it should average to a particular value say 7.5. How do I go about this?


Solution

  • If you want the expected value to be 7.5, just generate exponentials with a rate of 1/7.5 using an external library function or by implementing your own inverse transform routine.

    If you want the average of 100 values to be exactly 7.5, you can play some interesting games to achieve this. To get an average of 7.5 with 100 things, their sum must be 750. It turns out that distributing N points uniformly on a range R yields a Poisson process with rate N/R, and the distance between Poisson occurrences has an exponential distribution with the same rate. That leads to the following (and relatively simple) algorithm:

    1. Create an array poisson of length 101 containing the values 0 and 750.
    2. Fill the rest of the array by generating 99 additional numbers which are uniformly distributed between 0 and 750.
    3. Sort the array.
    4. Output poisson[i] - poisson[i-1] for i = 1,...,100.

    My Java is rusty, so I implemented it in a different language, but here are the results when analyzed with the statistics package JMP:

    Exponential distribution with average exactly equal to 7.5


    Here's my implementation in Ruby, which is practically pseudocode, in case you're interested. Should be easy to convert to Java.

    N = 100
    TARGET_MEAN = 7.5
    
    total = N * TARGET_MEAN
    poisson = [0, total]
    (N - 1).times { poisson << rand(0.0..total) }
    poisson.sort!
    (1...poisson.length).each { |i| p poisson[i] - poisson[i-1] }