Java Random Long nextLong()

Here you can find the source of nextLong()

Description

next Long

License

Open Source License

Declaration

public static long nextLong() 

Method Source Code

//package com.java2s;
/*//from   w w  w  . j  av  a  2  s .  c o  m
 * Created on Dec 30, 2005
 * Created by Alon Rohter
 * Copyright (C) Azureus Software, Inc, All Rights Reserved.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 */

import java.util.Random;

public class Main {
    public static final Random RANDOM = new Random(System.currentTimeMillis());

    public static long nextLong() {
        return RANDOM.nextLong();
    }

    public static long nextLong(long n) {
        if (n > Integer.MAX_VALUE) {

            while (true) {

                long rand = nextAbsoluteLong();

                long res = rand % n;

                // deal with non-uniformity as rand not generally divisible by n

                if (rand - res + (n - 1) >= 0) {

                    return (res);
                }
            }
        }

        return ((long) RANDOM.nextInt((int) n));
    }

    public static long nextAbsoluteLong() {
        return ((RANDOM.nextLong() << 1) >>> 1);
    }

    public static int nextInt(int n) {
        return RANDOM.nextInt(n);
    }

    public static int nextInt() {
        return RANDOM.nextInt();
    }
}

Related

  1. getLongSpecial()
  2. getLongSpecialCases()
  3. getRandomLong()
  4. nextLong()
  5. nextLong()
  6. nextLong(long n)
  7. nextLong(long n)
  8. nextLong(long RangeBottom, long rangeTop)
  9. nextLong(Random random, final long lower, final long upper)