Exemplo n.º 1
0
 /** {@inheritDoc} */
 public long nextSecureLong(final long lower, final long upper) throws NumberIsTooLargeException {
   if (lower >= upper) {
     throw new NumberIsTooLargeException(
         LocalizedFormats.LOWER_BOUND_NOT_BELOW_UPPER_BOUND, lower, upper, false);
   }
   final RandomGenerator rng = getSecRan();
   final long max = (upper - lower) + 1;
   if (max <= 0) {
     // the range is too wide to fit in a positive long (larger than 2^63); as it covers
     // more than half the long range, we use directly a simple rejection method
     while (true) {
       final long r = rng.nextLong();
       if (r >= lower && r <= upper) {
         return r;
       }
     }
   } else if (max < Integer.MAX_VALUE) {
     // we can shift the range and generate directly a positive int
     return lower + rng.nextInt((int) max);
   } else {
     // we can shift the range and generate directly a positive long
     return lower + nextLong(rng, max);
   }
 }