3

eeesh that title isn't very good.

Here is the situation.

I have a simulation that runs in real time, I'm trying to efficiently simulate a random count rate that has a known expectation value over a specific delta time and the probability distribution has a known standard deviation, variance etc etc...

I only want an integer number of counts, that is, for consecutive delta times I get results like 2, 1, 0, 3, 1, 2, 0, 1.

Currently I just multiply the number of counts per second by the delta time in seconds and add this to a running counts variable, and for displaying I round it down by casting to int from float. This won't cut the mustard in the end =(

I'm running ~60 fps and don't want to kill peoples processors if I can help it.

What would be a good way to accomplish this efficiently (I'm using Java)?

Neilos
  • 2,696
  • 4
  • 25
  • 51
  • Is your concern generating the numebrs (which I answered below) or how to efficiently pass them to the application? – user949300 Jan 13 '12 at 17:31
  • Thanks for your suggestion, I've been looking at this method today. The problem with Random.nextGaussian() is that it doesn't give out whole int values, let's say; normally I get 0 counts and every so often I get 1 count (maybe 1 in 100 times) using the .nextGaussian() solution I get a lot of values like 0.0001 which I round down to 0s because I need whole ints and that puts me back to square one. I realised that what I am describing is a _Poisson Distribution_ I found [this](http://acs.lbl.gov/software/colt/api/index.html) class and am currently trying to evaluate it, not there yet though... – Neilos Jan 13 '12 at 19:41
  • 1
    Poisson is very different! Let me know if you find a good implementation of that. Also, just noticed this [old SO discussion](http://stackoverflow.com/questions/750325/java-generator-for-poisson-and-uniform-distributions) and [another](http://stackoverflow.com/questions/1241555/algorithm-to-generate-poisson-and-binomial-random-numbers) – user949300 Jan 13 '12 at 19:50
  • With random functions you always get a floating point value between 0 and 1; to get an integer between 0 and X from that you just need to multiply by X, then cast to int to round the value. – Viruzzo Jan 13 '12 at 20:05

2 Answers2

2

It sounds like an Exponential distribution / Poisson process may be appropriate. An exponential distribution models the time between occurrences of a discrete event. The lambda parameter represents the rate of arrival of events. The Poisson process provides an alternate representation of the same underlying process, giving you the count (integer, from 0 to +inf) of events that happened in a certain timeframe.
The Poisson distribution is fairly easy to simulate. The lambda of Poisson represents the expected number of events in a time interval, and can be simulated that way.
One way to validate whether this is an adequate way to represent/simulate your process is to verify that the mean, variance etc... satisfy what they should be, if it were to follow such a process.

Mathias
  • 15,191
  • 9
  • 60
  • 92
  • I have indeed gone with a Poisson distribution, I went with the one in the Cern Colt Library for now as I cannot be bothered to write my own ^^ maybe I will in the future. Thanks to everyone for their assistance. – Neilos Jan 13 '12 at 23:37
0

Check out Random.nextGaussian(). It returns a random number with mean 0.0 and S.D. 1.0. You can use it, with scaling and offsets, to generate your data.

e.g.if you want a mean of 7 with S.D. 3, use

Random myRandom = new Random();
...

Double nextValue = 7.0 + myRandom.nextGaussian()*3.0;
joran
  • 169,992
  • 32
  • 429
  • 468
user949300
  • 15,364
  • 7
  • 35
  • 66
  • A Gaussian is not appropriate for a counting process, if only because it is not integer. On top of that it spans -infinity, +infinity. You could address these issues by using absolute values and rounding, but this is fundamentally trying to peg a square into a round hole. – Mathias Jan 13 '12 at 20:24
  • Yes. But OP hadn't yet clarified when I made this answer. – user949300 Jan 13 '12 at 21:16
  • Indeed I hadn't made it completely clear, the Gaussian method was not appropriate but still very nicely described. – Neilos Jan 13 '12 at 23:34