code: private static long nextLong(Random rng, long n) {
          if (n <= 0) throw new IllegalArgumentException("n must be greater than 0");
          long bits, val;
          do {
              bits = (rng.nextLong() << 1) >>> 1;
              val = bits % n;
          } while (bits - val + (n - 1) < 0L);
          return val;
      }
docs: Access the next long random number between 0 and n.
  
  @param rng random number generator
  @param n   max value
  @return a long random number between 0 and n
  @see <a href="http://stackoverflow.com/questions/2546078/java-random-long-number-in-0-x-n-range">Random Long Number in range, Stack Overflow</a>