java 产生指定范围的随机数

Math.random()

Math.random() 能够产生一个 大于等于 0 且 小于 1 的双精度伪随机数,假设须要产生 ”0<= 随机数 <=10” 的随机数,能够这样作:html

int num =(int)(Math.random() * 11);

那如何产生 “5<= 随机数 <= 10” 的随机数呢?java

int num = 5 + (int)(Math.random() * 6);

生成 “min <= 随机数 <= max ” 的随机数apache

int num = min + (int)(Math.random() * (max-min+1));

java.util.Random

Random 是 java 提供的一个伪随机数生成器。api

生成 “min <= 随机数 <= max ” 的随机数:oracle

import java.util.Random;

/**
 * Returns a pseudo-random number between min and max, inclusive.
 * The difference between min and max can be at most
 * <code>Integer.MAX_VALUE - 1</code>.
 *
 * @param min Minimum value
 * @param max Maximum value.  Must be greater than min.
 * @return Integer between min and max, inclusive.
 * @see java.util.Random#nextInt(int)
 */
public static int randInt(int min, int max) {

    // NOTE: Usually this should be a field rather than a method
    // variable so that it is not re-seeded every call.
    Random rand = new Random();

    // nextInt is normally exclusive of the top value,
    // so add 1 to make it inclusive
    int randomNum = rand.nextInt((max - min) + 1) + min;

    return randomNum;
}

标准库

在实际使用中,没有必要区从新写一次这些随机数的生成规则,能够借助一些标准库完成。如 commons-lang.dom

org.apache.commons.lang3.RandomUtils 提供了以下产生指定范围的随机数方法:this

// 产生 start <= 随机数 < end 的随机整数
public static int nextInt(final int startInclusive, final int endExclusive);
// 产生 start <= 随机数 < end 的随机长整数
public static long nextLong(final long startInclusive, final long endExclusive);
// 产生 start <= 随机数 < end 的随机双精度数
public static double nextDouble(final double startInclusive, final double endInclusive);
// 产生 start <= 随机数 < end 的随机浮点数
public static float nextFloat(final float startInclusive, final float endInclusive);

org.apache.commons.lang3.RandomStringUtils 提供了生成随机字符串的方法,简单介绍一下:spa

// 生成指定个数的随机数字串
public static String randomNumeric(final int count);
// 生成指定个数的随机字母串
public static String randomAlphabetic(final int count);
// 生成指定个数的随机字母数字串
public static String randomAlphanumeric(final int count);
相关文章
相关标签/搜索