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

15,228

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.

Share:
15,228
Ivan
Author by

Ivan

Updated on June 07, 2022

Comments

  • Ivan
    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 for scala.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
    Luigi Plinge over 12 years
    On average, in fact, exactly 50% of cases
  • om-nom-nom
    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
    Luigi Plinge over 12 years
    I 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
    Fabien Warniez over 6 years
    what are the odds of that?! :)
  • missingfaktor
    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
    missingfaktor over 6 years
    I am a little bit spooked TBH!
  • Jim Schubert
    Jim Schubert over 6 years
    This answer from 2011 was the second search result when I looked for scala random int non-zero. Maybe it's a sign.
  • swifthorseman
    swifthorseman almost 6 years
    If 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.