Can scala.util.Random.nextInt (): Int occasionally return a negative value?
Solution 1
Apparently, yes. It returned a negative value on my first try! :-)
scala> import util.Random
import util.Random
scala> Random.nextInt
res0: Int = -299006430
Solution 2
Yes, you can (and that's ok due to definition of uniform distribution). Moreover you'll get it in nearly 50% of cases.
(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%.
Solution 3
As you can see here (using Mike Harrah's excellent sxr), Scala's Random
just delegates to an underlying java.util.Random
, which is referred to as self
.
As others pointed out the default range is between Integer.MIN_VAL
and Integer.MAX_VAL
, in other words, any Integer
possible, including the negative ones.
If you want just the positive range, you can use the overloaded method nextInt
that takes an argument, like this:
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.
Solution 4
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.
Ivan
Updated on June 07, 2022Comments
-
Ivan almost 2 years
Documentation for
scala.util.Random.nextInt (n: Int): Int
says "Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive)..." while forscala.util.Random.nextInt (): Int
it says "Returns the next pseudorandom, uniformly distributed int value...", without saying anything about the zero. Can I get a negative value here occasionally? -
Luigi Plinge over 12 yearsOn average, in fact, exactly 50% of cases
-
om-nom-nom over 12 years@Luigi Don't forget about neutral cases, when random value is equals to 0. So there is would be 49,9...% percents of positive, the same number of negatives and a few of zeros.
-
Luigi Plinge over 12 yearsI didn't forget... well, I'm assuming any value is equally likely. Maybe you can't get Int.MinValue or something. There are 2147483648 negative, 2147483647 positive, and 1 zero Int
-
Fabien Warniez over 6 yearswhat are the odds of that?! :)
-
missingfaktor over 6 years@FabienWarniez, what are the odds of you commenting this on my ancient post yesterday, and me running into similar situation at about the same time? :) twitter.com/missingfaktor/status/914642815854006272
-
missingfaktor over 6 yearsI am a little bit spooked TBH!
-
Jim Schubert over 6 yearsThis answer from 2011 was the second search result when I looked for
scala random int non-zero
. Maybe it's a sign. -
swifthorseman almost 6 yearsIf you want the positive range, you'd rather do:
Random.nextInt(Integer.MAX_VALUE+1)
, since 0 is not considered positive and Integer.MAX_VALUE is going to be excluded at the top end.