使用概率分布生成范围内的随机整数 [英] Generating random integers within range with a probability distribution
问题描述
我有一个问题,我想使用概率分布生成1到5之间的一组随机整数值。
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和Inverse Gamma是两个分布,显示了我所发现的特征(多数均值,更低的数字)。
Poisson and Inverse Gamma are two distributions that show the characteristics I am after (majority at mean, less higher numbers) that I have found.
我正在考虑使用 Apache Commons Math 但我我不知道如何使用可用的发行版生成我想要的数字。
I am looking at using Apache Commons Math but I wasn't sure how to generate the numbers I wanted using the distributions available.
推荐答案
从您的问题描述中,它听起来像你实际上想要一个离散概率分布生成的样本,你可以使用 EnumeratedIntegerDistribution
。为每个整数选择合适的概率,可能类似以下内容满足您的需求:
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);
只需将 discreteProbabilities
值调整为任何你要求。
Just tweak the discreteProbabilities
values to whatever you require.
这篇关于使用概率分布生成范围内的随机整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!