Java Random Long nextLong(Random rng, long n)

Here you can find the source of nextLong(Random rng, long n)

Description

next Long

License

Mozilla Public License

Declaration

public final static long nextLong(Random rng, long n) 

Method Source Code


//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;
    }
}

Related

  1. nextLong(long n)
  2. nextLong(long n)
  3. nextLong(long RangeBottom, long rangeTop)
  4. nextLong(Random random, final long lower, final long upper)
  5. nextLong(Random rng, long n)
  6. nextLongId()
  7. randLong()
  8. randLong(long minimum, long maximum)
  9. randLong(long n)