java Random.nextGaussian() 的问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/629798/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
problem with Random.nextGaussian()
提问by BHS
Random.nextGaussian() is supposed to give random no.s with mean 0 and std deviation 1. Many no.s it generated are outside range of [-1,+1]. how can i set so that it gives normally distributed random no.s only in the range -1 to 1.
Random.nextGaussian() 应该给出均值为 0 且标准偏差为 1 的随机数。它生成的许多数都在 [-1,+1] 的范围之外。我如何设置以便它只在 -1 到 1 的范围内给出正态分布的随机数。
回答by dirkgently
A Gaussian distribution with a mean 0 and standard deviation one means that the average of the distribution is 0 and about 70% of the population lies in the range [-1, 1]. Ignore the numbers that are outside your range -- they form the fringe 16% approx on either side.
均值为 0 且标准差为 1 的高斯分布意味着分布的平均值为 0,并且大约 70% 的总体位于 [-1, 1] 范围内。忽略超出范围的数字——它们在两边形成大约 16% 的边缘。
Maybe a better solution is to generate a distribution with mean=0and std.dev=0.5. This will give you a distribution with about 96% of the values in the range [-1, 1].
也许更好的解决方案是使用mean=0和生成分布std.dev=0.5。这将为您提供大约 96% 的值在 [-1, 1] 范围内的分布。
An even better solution is to work backward as above and use the idea that approx. 99.7% of the values lie in the 3-sigma range: use a std.dev = 1/3. That will almost nullify the amount of not-so-useful values that you are getting. When you do get one, omit it.
一个更好的解决方案是像上面那样向后工作并使用大约的想法。99.7% 的值位于 3-sigma 范围内:使用std.dev = 1/3. 这几乎会使您获得的不那么有用的值的数量无效。当你得到一个时,省略它。
Of course, if you are working on a math intensive product, all of this bears no value.
当然,如果您正在开发数学密集型产品,所有这些都没有任何价值。
回答by Ned Batchelder
Doesn't the normal distribution include numbers arbitrarily far from the mean, but with increasingly small probabilities? It might be that your desires (normal and limited to a specific range) are incompatible.
正态分布不包括任意远离均值但概率越来越小的数字吗?可能是您的愿望(正常且仅限于特定范围)不相容。
回答by Jon Skeet
A normal distribution gives a non-zero (but "becoming extremely small") probability of seeing values outside [-1, +1] whatever variance you give - you're just squishing the curve, effectively.
正态分布给出了一个非零(但“变得非常小”)的概率,可以看到 [-1, +1] 之外的值,无论你给出什么方差——你只是在有效地挤压曲线。
You could use a small variance and then just run the results through a map which cropped anything less than -1 to -1, and anything greater than 1 to 1, but it wouldn't (strictly speaking) be a normal distribution any more.
你可以使用一个小的方差,然后通过一个地图运行结果,该地图裁剪了小于 -1 到 -1 的任何东西,以及大于 1 到 1 的任何东西,但它(严格来说)不再是正态分布。
What do you need this distribution for, out of interest?
出于兴趣,您需要此分发做什么?
回答by SurDin
Gaussian distribution with your parameters. is has density e^(-x^2/2). In general it is of the form e^(linear(x)+linear(x^2)) which means whatever settings you give it, you have some probability of getting very large and very small numbers.
You are probably looking for some other distribution.
带参数的高斯分布。具有密度 e^(-x^2/2)。一般来说,它的形式是 e^(linear(x)+linear(x^2)) 这意味着无论你给它什么设置,你都有可能得到非常大和非常小的数字。
您可能正在寻找其他一些发行版。
回答by Hardzsi
This code will display countnumber of random Gaussian numbers to console (10 in a line) and shows you some statistics (lowest, highest and average) afterwards.
此代码将向控制台显示随机高斯数的计数(一行 10),然后显示一些统计数据(最低、最高和平均值)。
If you try it with small countnumber, random numbers will be probably in range [-1.0 ... +1.0] and average can be in range [-0.1 ... +0.1]. However, if countis above 10.000, random numbers will fall probably in range [-4.0 ... +4.0] (more improbable numbers can appear on both ends), although average can be in range [-0.001 ... +0.001] (way closer to 0).
如果您尝试使用小计数,随机数可能在 [-1.0 ... +1.0] 范围内,平均值可能在 [-0.1 ... +0.1] 范围内。但是,如果计数高于 10.000,随机数可能会落在 [-4.0 ... +4.0] 范围内(两端可能出现更不可能的数字),尽管平均值可能在 [-0.001 ... +0.001] 范围内(更接近于 0)。
public static void main(String[] args) {
int count = 20_000; // Generated random numbers
double lowest = 0; // For statistics
double highest = 0;
double average = 0;
Random random = new Random();
for (int i = 0; i < count; ++i) {
double gaussian = random.nextGaussian();
average += gaussian;
lowest = Math.min(gaussian, lowest);
highest = Math.max(gaussian, highest);
if (i%10 == 0) { // New line
System.out.println();
}
System.out.printf("%10.4f", gaussian);
}
// Display statistics
System.out.println("\n\nNumber of generated random values following Gaussian distribution: " + count);
System.out.printf("\nLowest value: %10.4f\nHighest value: %10.4f\nAverage: %10.4f", lowest, highest, (average/count));
}
回答by Tor Haugen
A standard deviation of 1.0 entails that many values will lie outside the [-1,1] range.
1.0 的标准偏差意味着许多值将位于 [-1,1] 范围之外。
If you need to keep within this range, you should use another method, perhaps nextDouble().
如果你需要保持在这个范围内,你应该使用另一种方法,也许是 nextDouble()。

