(int limit)
| 43 | } |
| 44 | |
| 45 | public int nextInt(int limit) { |
| 46 | if (limit <= 0) { |
| 47 | throw new IllegalArgumentException(); |
| 48 | } |
| 49 | |
| 50 | if ((limit & -limit) == limit) { |
| 51 | // limit is a power of two |
| 52 | return (int) ((limit * (long) next(31)) >> 31); |
| 53 | } |
| 54 | |
| 55 | int bits; |
| 56 | int value; |
| 57 | do { |
| 58 | bits = next(31); |
| 59 | value = bits % limit; |
| 60 | } while (bits - value + (limit - 1) < 0); |
| 61 | |
| 62 | return value; |
| 63 | } |
| 64 | |
| 65 | public int nextInt() { |
| 66 | return next(32); |