All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.dexcoder.commons.utils.RandomUtils Maven / Gradle / Ivy

The newest version!
package com.dexcoder.commons.utils;

import java.util.Random;

/**
 * 本类来源于apache-commons-lang > RandomUtils
 *
 * Created by liyd on 2015-12-4.
 */
public class RandomUtils {

    /**
     */
    public static final Random JVM_RANDOM = new Random();

    // should be possible for JVM_RANDOM?
    //    public static void nextBytes(byte[]) {
    //    public synchronized double nextGaussian();
    //    }

    /**
     * 

Returns the next pseudorandom, uniformly distributed int value * from the Math.random() sequence.

* N.B. All values are >= 0. * * @return the random int */ public static int nextInt() { return nextInt(JVM_RANDOM); } /** * 指定区间的随机数 * * @param min * @param max * @return */ public static int nextInt(int min, int max) { return JVM_RANDOM.nextInt((max - min) + 1) + min; } /** *

Returns the next pseudorandom, uniformly distributed int value * from the given random sequence.

* * @param random the Random sequence generator. * @return the random int */ public static int nextInt(Random random) { return random.nextInt(); } /** *

Returns a pseudorandom, uniformly distributed int value * between 0 (inclusive) and the specified value * (exclusive), from the Math.random() sequence.

* * @param n the specified exclusive max-value * @return the random int */ public static int nextInt(int n) { return nextInt(JVM_RANDOM, n); } /** *

Returns a pseudorandom, uniformly distributed int value * between 0 (inclusive) and the specified value * (exclusive), from the given Random sequence.

* * @param random the Random sequence generator. * @param n the specified exclusive max-value * @return the random int */ public static int nextInt(Random random, int n) { // check this cannot return 'n' return random.nextInt(n); } /** *

Returns the next pseudorandom, uniformly distributed long value * from the Math.random() sequence.

* N.B. All values are >= 0. * * @return the random long */ public static long nextLong() { return nextLong(JVM_RANDOM); } /** *

Returns the next pseudorandom, uniformly distributed long value * from the given Random sequence.

* * @param random the Random sequence generator. * @return the random long */ public static long nextLong(Random random) { return random.nextLong(); } /** *

Returns the next pseudorandom, uniformly distributed boolean value * from the Math.random() sequence.

* * @return the random boolean */ public static boolean nextBoolean() { return nextBoolean(JVM_RANDOM); } /** *

Returns the next pseudorandom, uniformly distributed boolean value * from the given random sequence.

* * @param random the Random sequence generator. * @return the random boolean */ public static boolean nextBoolean(Random random) { return random.nextBoolean(); } /** *

Returns the next pseudorandom, uniformly distributed float value * between 0.0 and 1.0 from the Math.random() * sequence.

* * @return the random float */ public static float nextFloat() { return nextFloat(JVM_RANDOM); } /** *

Returns the next pseudorandom, uniformly distributed float value * between 0.0 and 1.0 from the given Random * sequence.

* * @param random the Random sequence generator. * @return the random float */ public static float nextFloat(Random random) { return random.nextFloat(); } /** *

Returns the next pseudorandom, uniformly distributed float value * between 0.0 and 1.0 from the Math.random() * sequence.

* * @return the random double */ public static double nextDouble() { return nextDouble(JVM_RANDOM); } /** *

Returns the next pseudorandom, uniformly distributed float value * between 0.0 and 1.0 from the given Random * sequence.

* * @param random the Random sequence generator. * @return the random double */ public static double nextDouble(Random random) { return random.nextDouble(); } }