code: public static long nextLong(Random random, long n) {
          // This code is based on java.util.Random#nextInt(int)'s javadoc.
          if (n <= 0L) {
              throw new IllegalArgumentException("n must be positive");
          }
          if (n < Integer.MAX_VALUE) {
              return (long) random.nextInt((int) n);
          }
  
          long bits;
          long val;
          do {
              bits = (random.nextLong() << 1) >>> 1;
              val = bits % n;
          } while (bits - val + (n - 1L) < 0L);
          return val;
      }
docs: Mimics {@link Random#nextInt(int)} for longs.
  @param random never null
  @param n {@code > 0L}
  @return like {@link Random#nextInt(int)} but for a long
  @see Random#nextInt(int)