scala.util.Random.nextInt():Int 可以偶尔返回负值吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7783902/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 03:35:35  来源:igfitidea点击:

Can scala.util.Random.nextInt (): Int occasionally return a negative value?

scalarandom

提问by Ivan

Documentation for scala.util.Random.nextInt (n: Int): Intsays "Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive)..." while for scala.util.Random.nextInt (): Intit says "Returns the next pseudorandom, uniformly distributed int value...", without ssaying anything about the zero. Can I get a negative value here occasionally?

文档scala.util.Random.nextInt (n: Int): Int说“返回一个伪随机,均匀分布的 int 值介于 0(包括)和指定的值(不包括在内)......”而scala.util.Random.nextInt (): Int它说“返回下一个伪随机,均匀分布的 int 值......”,没有说任何话关于零。我偶尔会在这里得到负值吗?

回答by om-nom-nom

Yes, you can (and that's ok due to definitionof uniform distribution). Moreover you'll get it in nearly 50% of cases.

是的,您可以(由于均匀分布的定义,这没关系)。此外,您将在近 50% 的情况下获得它。

(for(i <- 1 to 100000) yield scala.util.Random.nextInt()).filter(_<0).length

have yielded for me 49946 - that's quite close to the 50%.

已经为我产生了 49946 - 非常接近 50%。

回答by missingfaktor

Apparently, yes. It returned a negative value on my first try! :-)

显然,是的。我第一次尝试时它返回了一个负值!:-)

scala> import util.Random
import util.Random

scala> Random.nextInt
res0: Int = -299006430

回答by Pablo Fernandez

As you can see here(using Mike Harrah's excellent sxr), Scala's Randomjust delegates to an underlying java.util.Random, which is referred to as self.

正如您在此处看到的(使用 Mike Harrah 出色的sxr),ScalaRandom只是将 委托给了一个底层java.util.Random,称为self

As others pointed out the default range is between Integer.MIN_VALand Integer.MAX_VAL, in other words, any Integerpossible, including the negative ones.

正如其他人指出的那样,默认范围在Integer.MIN_VAL和之间Integer.MAX_VAL,换句话说,任何Integer可能的范围,包括负数。

If you want just the positive range, you can use the overloaded method nextIntthat takes an argument, like this:

如果您只想要正范围,则可以使用nextInt带参数的重载方法,如下所示:

Random.nextInt(Integer.MAX_VALUE);

According to the docs:

根据文档:

Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence.

返回一个伪随机、均匀分布的 int 值,介于 0(包含)和指定值(不包含)之间,从该随机数生成器的序列中抽取。

回答by Clark Bao

scala.uti.Random.nextInt (): Int leverages the same method of java.util.Random. And the range as Luigi pointed out is [Integer.MIN_VAL,Integer.MAX_VAL]. Actually "uniformly distributed int value" means any number of int type is possible to be returned and the chance of each one in theory to be the same.

scala.uti.Random.nextInt():Int 利用了与 java.util.Random 相同的方法。Luigi 指出的范围是 [Integer.MIN_VAL,Integer.MAX_VAL]。实际上“均匀分布的 int 值”意味着可以返回任意数量的 int 类型,并且理论上每个类型的机会都是相同的。