0
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

  • 2
    Please take time to ask a question next time – Scary Wombat Mar 10 '17 at 01:40
  • @ScaryWombat Is anything not understandable? – Sarabjeet Singh Mar 10 '17 at 01:43
  • you don't really ask a question. For the record where does the `ExponentialDistribution` class come from? A simple solution is if the number does not meet your requirements then don't use it, look for another number. Also if you are adding `i` (the index) then as the index grows so will the number – Scary Wombat Mar 10 '17 at 01:50
  • @ScaryWombat Exponantial distribution comes from this package: org.apache.commons.math3.distribution.ExponentialDistribution.... I know if the index increases, number automatically increases. I want that number should not increase instead their difference changes. eg: I have array of index [abc] of index 5. From above above code it fills {2,4,6,8,10}. If i increase index to 10, it fills {2,4,6,......,20}. Instead, I want {1,2,3,4,.....,10}. Hope this makes some sense. P.S this is just an example, not real values – Sarabjeet Singh Mar 10 '17 at 02:01
  • `int val = (int)exp.sample() + 1 + r[i-1];` loop `is val > 5000 try again` – Scary Wombat Mar 10 '17 at 02:08
  • @ScaryWombat Could be, because its randomly generated in every run. I am looking for the solution to limit it to 5000, if this is the case. – Sarabjeet Singh Mar 10 '17 at 02:12
  • maybe just use `Random` then - see http://stackoverflow.com/questions/363681/how-to-generate-random-integers-within-a-specific-range-in-java – Scary Wombat Mar 10 '17 at 02:14

1 Answers1

0

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.

sprinter
  • 27,148
  • 6
  • 47
  • 78