Here you can find the source of nextLong(Random rng, long n)
public final static long nextLong(Random rng, long n)
//package com.java2s; /*/* w ww . j a v a 2 s. c o m*/ * Copyright (c) 2014 mucaho (https://github.com/mucaho). * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ import java.util.Random; public class Main { public final static long nextLong(Random rng, long n) { // error checking and 2^x checking removed for simplicity. long bits, val; do { bits = (rng.nextLong() << 1) >>> 1; val = bits % n; } while (bits - val + (n - 1) < 0L); return val; } }