Generating random integers within range with a probability distribution

前端 未结 2 696
旧巷少年郎
旧巷少年郎 2020-12-31 22:48

I have a problem where I want to generate a set of random integer values between 1 and 5 inclusive using a probability distribution.

Poisson and Inverse Gamma are t

2条回答
  •  盖世英雄少女心
    2020-12-31 23:10

    From your problem description, it sounds like you actually want a sample generated from a discrete probability distribution, and you can use EnumeratedIntegerDistribution for this purpose. Choose appropriate probabilities for each of your integers, maybe something like the following would meet your needs:

    int[] numsToGenerate           = new int[]    { 1,   2,    3,   4,    5   };
    double[] discreteProbabilities = new double[] { 0.1, 0.25, 0.3, 0.25, 0.1 };
    
    EnumeratedIntegerDistribution distribution = 
        new EnumeratedIntegerDistribution(numsToGenerate, discreteProbabilities);
    
    int numSamples = 100;
    int[] samples = distribution.sample(numSamples);
    

    Just tweak the discreteProbabilities values to whatever you require.

提交回复
热议问题