简体   繁体   中英

Fill an array with random numbers within a specific range using exponantial distribution

int[] r = new int[1000];

ExponentialDistribution exp = new ExponentialDistribution(4.0);

for(int i = 1; i <r.length; i++){
    r[i] = (int)exp.sample()  + 1 + r[i-1];
}

The above code fills array [r] from r[0] = 0 to r[999] = 4527. This is the result of one random run. If r.length is increased to 2000, the last element r[1999] increases 8963. I am trying to find the solution to fill the array [r] within range 0 - 5000. Even if r.length is increased, the array should be filled in this range exponentially.Say in this case, if r[999] <= 5000, then r[1999] should also be <=5000.

Say, r.length represents total no. of events and each element of array r represents the time at which event occurs.Total time is 5000 units. Motive is that first event happens at time 0 and last at time <=5000 even if r.length is increased or decreased that is inter occurrence time of two events should be adjusted accordingly.

Thanks a lot

I think what you are asking for is: fill an array of an arbitrary length with numbers representing the time at which an event occurs. The time of the last event should be less than a given amount.

If you are really simulating events arriving then this doesn't make a lot of sense. See https://en.wikipedia.org/wiki/Arrival_theorem for lots of detail about why not. But, in summary, you would normally decide how often events occur (on average) and how many events you want in the list. To get a realistic distribution that happens to have a given number of events ending in a given time you would probably need to run the generation with no limit then scale back all the event times. But honestly I'm not sure what the point of that would be.

Alternatively, if you just want a set of numbers between two ranges that are sorted in ascending order then that's pretty easy:

int[] events = random.ints(count, min, max).sorted().toArray();

This would not be a realistic simulation of a poisson distribution but without further information about what you are trying to achieve (and why you think an exponential distribution is required) then it's hard to give more specific advice.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM