Jon Thoms
Jon Thoms

Reputation: 10739

ThreadLocalRandom nextInt Without Range

Does ThreadLocalRandom's nextInt() method (i.e. the nextInt() method without any parameters) actually generate a pseudo-random integer without a range (i.e. between Integer.MIN_VALUE and Integer.MAX_VALUE) or is there some implicit range (e.g. only non-negative integers)?

The Javadoc doesn't explicitly state whether or not an implicit range is used but the source code for ThreadLocalRandom seems to indicate that no range is used as ThreadLocalRandom's nextInt() method is actually java.util.Random's nextInt() method, which calls java.util.Random.next(32).

I'm fairly confident that any integer is pseudo-randomly generated using this method but I thought that I would double check with folks who are more familiar with this code and bit twiddling in general than I am. Thanks.

Upvotes: 0

Views: 482

Answers (1)

Stefan Zobel
Stefan Zobel

Reputation: 3212

The Javadoc says everything about it:

"All 2^32 possible int values are produced with (approximately) equal probability".

Is there something more to know?

Upvotes: 2

Related Questions