泊松分布和均匀分布的 Java 生成器?
据我了解,标准生成器适用于正态分布。 我必须根据正态分布、均匀分布和泊松分布生成随机数,但我似乎找不到最后 2 个类别。
我必须在 0 - 999999 的范围内生成它们。
From what I understand, the standard generator is for the Normal Distribution. I have to generate random numbers according to the Normal, Uniform and Poisson Distributions, but I can't seem to find a class for the last 2.
I have to generate them in the range of 0 - 999999.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
正如 David 指出的,提供的伪随机数生成器使用均匀分布。
对于另外两个,我将使用 Cern Colt 库函数:
这些库函数可以轻松地让您找到从每个分布中获取的随机数,而不是给你一个概率密度函数或累积密度函数并期望你自己导出数字(这似乎是 Apache Commons-Math 方法):
另外,请记住,大 λ 的泊松分布 P(λ) 可以可以很好地近似于正态分布 N(λ, sqrt(λ))。
As David has pointed out, the supplied pseudo-random number generator uses the Uniform distribution.
For the other two, I would use the Cern Colt library functions:
These library functions easily allow you to find a random number taken from each distribution, rather than giving you a probability density function or cumulative density function and expecting you to derive the number yourself (which seems to be the Apache Commons-Math approach):
Also, bear in mind that the Poisson distribution P(λ) for large λ can be approximated very well by the normal distribution N(λ, sqrt(λ)).
标准 Java RNG (java.util.Random) 及其子类,例如 java.security .SecureRandom,已经生成均匀分布的值。
他们还有一个方法, nextGaussian< /a>,返回正态分布值。 默认情况下,分布的均值为零,标准差为 1,但这可以进行简单的调整。 只需乘以所需的标准差并加上所需的平均值即可。 因此,举例来说,如果您想要平均值为 6、标准差为 2.5 的正态分布值,您可以这样做:
没有明确支持泊松分布,但您可以通过执行与 Tom 的 Python 代码。
或者,您可能对我的Uncommons Maths 库感兴趣,它提供了正态分布、泊松分布和其他分布的实用程序类。
The standard Java RNG (java.util.Random), and its subclasses such as java.security.SecureRandom, already generate uniformly distributed values.
They also have a method, nextGaussian, that returns normally-distributed values. By default, the distribution has mean of zero and standard deviation of 1 but this is trivially tweaked. Just multiply by the required s.d. and add the required mean. So, for example, if you wanted normally-distributed values with a mean of 6 and standard deviation of 2.5, you'd do this:
The Poisson distribution is not explicitly supported, but you can fake it by doing the same as Tom's Python code.
Alternatively, you may be interested in my Uncommons Maths library, which provides utility classes for Normal, Poisson and other distributions.
实际上,标准生成器是为了统一分配。 任何语言/库中的基本随机数生成器将始终(在我知道的所有情况下)使用均匀分布,因为这是所有流行的伪随机数生成器算法的结果 - 基本上,均匀随机数是最简单的。
我看到埃迪已经向您指出了其他发行版的链接,所以我将跳过编写其余部分......
Actually, the standard generator is for the uniform distribution. The basic random number generator in any language/library will always (in all cases I know of) use the uniform distribution because that's what comes out of all the popular pseudorandom number generator algorithms - basically, uniform random numbers are the easiest.
I see Eddie already pointed you to a link for other distributions so I'll skip writing the rest of this...
让我先说明一下,这些都不是真正随机的,我说的是伪随机数生成器。
我还要说的是,我从来没有必要为生产质量代码这样做。 不过,我已经用 Python 完成了硬件作业。 我模拟了泊松随机变量。
我的做法利用了以下事实:
特别是,您可以使用以下事实:如果 X1, ..., Xn 是独立的标准指数随机变量,则 Z = min(k : X1 + ... + Xk < λ) - 1 是泊松 (λ)。
因此,我在 python 中编写了以下代码来生成泊松值:
该类的示例用法是:
我将其发布在这里,因为很高兴知道这些类型的关系存在,并且此逆变换方法为您提供了一个通用的方法处理遵循特定连续分布的随机值的方法。
Let me preface all this by the fact that none of this is truly random, I am talking about pseudo random number generators.
Let me also say that I have never had to do this for production quality code. I have done this for a hw assignment though, in Python. I simulated Poisson random variables.
The way that I did it made use of the following facts:
In particular, you can use the fact that: if X1, ..., Xn are independent standard exponential random variables, then Z = min(k : X1 + ... + Xk < λ) - 1 is Poisson(λ).
So, with that, I wrote the following code in python to generate Poisson values:
Example usage of the class is:
I posted this here because it is good to know that these kinds of relationships exist, and this inverse transform method gives you a general way to deal with generating random values following a particular continuous distribution.