简体   繁体   中英

ThreadLocalRandom nextInt Without Range

Does ThreadLocalRandom 's nextInt() method (ie the nextInt() method without any parameters) actually generate a pseudo-random integer without a range (ie between Integer.MIN_VALUE and Integer.MAX_VALUE ) or is there some implicit range (eg 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.

The Javadoc says everything about it:

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

Is there something more to know?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM