10

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?

thebluephantom
  • 16,458
  • 8
  • 40
  • 83
Ivan
  • 63,011
  • 101
  • 250
  • 382
  • 1
    Where does it say that? The docs I found said "All 2^32 possible int values are produced with (approximately) equal probability", which means that negative numbers are *more* likely than positive ones (since there is one more negative number). – Michael Lorton Oct 16 '11 at 17:18

4 Answers4

12

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%.

om-nom-nom
  • 62,329
  • 13
  • 183
  • 228
  • On average, in fact, *exactly* 50% of cases – Luigi Plinge Oct 16 '11 at 12:28
  • 2
    @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. – om-nom-nom Oct 16 '11 at 12:32
  • 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 – Luigi Plinge Oct 16 '11 at 12:34
10

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

scala> import util.Random
import util.Random

scala> Random.nextInt
res0: Int = -299006430
missingfaktor
  • 90,905
  • 62
  • 285
  • 365
10

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.

Mark Canlas
  • 9,385
  • 5
  • 41
  • 63
Pablo Fernandez
  • 103,170
  • 56
  • 192
  • 232
  • 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. – swifthorseman Jun 20 '18 at 10:30
2

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.

Clark Bao
  • 1,743
  • 3
  • 21
  • 39