generate normal distributed timestamps within a range [0,x]

依然范特西╮ 提交于 2019-12-13 18:18:15

问题


I want to generate a file containing timestamps (integers between 0 and a bound value x, in increasing order) which represents arrivals of an event. The "Event arrival rate" should be "normal distributed" which means, somehow in the "middle" of the dataset the rate of arrivals should be more frequently as at the beginning and the end. How can i generate such a list of values using java?

regards


回答1:


I agree with greedybuddha that a Gaussian function is what you want here, but you also stated that you want your events to be ordered - Random.nextGaussian() won't give you that, it will give you random numbers that are normally distributed. Instead, use the gaussian function to calculate the frequency of events at each point in time:

for (int t = 0; t < max; t++)
{
    f = Math.exp(-Math.pow(t - CENTER, 2.0) / (2.0 * Math.pow(WIDTH, 2.0)));
    for (int j = 0; j < f; j++)
    {
        writeEvent(t);
    }
}

CENTER is where you want the "peak" of the curve to be (probably max/2), and WIDTH is a parameter that controls the spread of the distribution.




回答2:


Java has a Random class and one of it's methods is a nextGaussian which will give you a normal distribution from 0-1.0 (Gaussian Distribution and Normal Distribution are synonyms).

From there you just need to multiply that by your range to get a value from that range.

Random random = new Random();
public int nextNormalTime(int upperTimeBound){
    return (int)(random.nextGaussian()*upperTimeBound);
}

If you want to create an ordered list of these, you can just add the times into a list and sort, or into something like a PriorityQueue.

List<Integer> list = new ArrayList<Integer>(nTimes);
for (int i=0;i<nTimes;i++){
    list.add(nextNormalTime(upperTimeBound));
}
Collections.sort(list);


来源:https://stackoverflow.com/questions/16828738/generate-normal-distributed-timestamps-within-a-range-0-x

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!