【知识积累】随机数生成的几种方法

一、前言

  在我们平时写代码的时候,免不了会使用到随机数,特此将几种随机的生成总结如下。

二、随机数生成

  对于随机数的生成,分为四种情况,假设两个数为min, max,则有如下四种情况。

  1. (min, max),表示生成的随机数不包括min和max。

  2. [min, max),表示生成的随机数包括min,但不包括max。

  3. (min, max],表示生成的随机数不包括min,但是包括max。

  4. [min, max],表示生成的随机数包min,也包括max。

  下面我们就上面的四种情况使用三种不同的方法实现。

  2.1 使用Math.random方法

  其代码如下  

package com.hust.grid.leesf.random;

/**
 * 使用Math.random方法生成随机数
 * @author LEESF
 * 2016.3.30
 */
public class RandomTest {
    //(min, max)
    public static int random1(int min, int max) {
        int ran;
        while ((ran = (int) (Math.random() * (max - min) + min)) == min);
        return ran;
    }
    
    //[min, max)
    public static int random2(int min, int max) {
        int ran = (int) (Math.random() * (max - min) + min);
        return ran;
    }
    
    // (min, max]
    public static int random3(int min, int max) {
        int ran;
        while ((ran = (int) (Math.random() * (max - min + 1) + min)) == min);
        return ran;
    }
    
    //[min, max] 
    public static int random4(int min, int max) {
        int ran = (int) (Math.random() * (max - min + 1) + min);
        return ran;
    }
    
    public static void main(String[] args) {
        int min = 40;
        int max = 100;
        // (min, max)
        System.out.println(random1(min, max));
        // [min, max)
        System.out.println(random2(min, max));
        // (min, max]
        System.out.println(random3(min, max));
        // [min, max]
        System.out.println(random4(min, max));
    }
}
View Code

  运行结果 

59
49
57
45
View Code

  2.2 使用Random对象的nextInt方法

  其代码如下 

package com.hust.grid.leesf.random;

import java.util.Random;

/**
 * 使用Random对象生成随机数
 * 
 * @author LEESF 2016.3.30
 */
public class RandomTest {
    // (min, max)
    public static int random1(int min, int max) {
        Random random = new Random();
        int seed = max - min;
        int ran;
        while ((ran = random.nextInt(seed) + min) == min)
            ;
        return ran;
    }

    // [min, max)
    public static int random2(int min, int max) {
        Random random = new Random();
        int seed = max - min;
        int ran = random.nextInt(seed) + min;
        return ran;
    }

    // (min, max]
    public static int random3(int min, int max) {
        Random random = new Random();
        int seed = max - min + 1;
        int ran;
        while ((ran = (int) (random.nextInt(seed) + min)) == min)
            ;
        return ran;
    }

    // [min, max]
    public static int random4(int min, int max) {
        Random random = new Random();
        int seed = max - min + 1;
        int ran = random.nextInt(seed) + min;
        return ran;
    }

    public static void main(String[] args) {
        int min = 40;
        int max = 100;
        // (min, max)
        System.out.println(random1(min, max));
        // [min, max)
        System.out.println(random2(min, max));
        // (min, max]
        System.out.println(random3(min, max));
        // [min, max]
        System.out.println(random4(min, max));
    }
}
View Code

  运行结果  

76
63
66
93
View Code

  2.3 使用System类的currentTimeMillis方法

  这种方式的随机数不是随机的,但是在不严格的情况可以使用,可以用作参考,代码如下

package com.hust.grid.leesf.random;


/**
 * 使用System类生成随机数
 * 
 * @author LEESF 2016.3.30
 */
public class RandomTest {
    // (min, max)
    public static int random1(int min, int max) {
        int random;
        while ((random = (int) (System.currentTimeMillis() % (max - min) + min)) == min)
            ;
        return random;
    }

    // [min, max)
    public static int random2(int min, int max) {
        long currentTime = System.currentTimeMillis();
        int random = (int) (currentTime % (max - min));
        return random;
    }

    // (min, max]
    public static int random3(int min, int max) {
        int random;
        while ((random = (int) (System.currentTimeMillis() % (max - min + 1) + min)) == min)
            ;
        return random;
    }

    // [min, max]
    public static int random4(int min, int max) {
        int random = (int) (System.currentTimeMillis() % (max - min + 1) + min);
        return random;
    }

    public static void main(String[] args) {
        int min = 40;
        int max = 100;
        // (min, max)
        System.out.println(random1(min, max));
        // [min, max)
        System.out.println(random2(min, max));
        // (min, max]
        System.out.println(random3(min, max));
        // [min, max]
        System.out.println(random4(min, max));
    }
}
View Code

  运行结果

65
25
62
62
View Code

三、总结

  对随机数生成的几种方法进行了总结,在以后需要的时候直接可以使用,平时多进行积累。谢谢各位园友的观看~

原文地址:https://www.cnblogs.com/leesf456/p/5338666.html