【知識積累】隨機數生成的幾種方法

leesf發表於2016-03-30

一、前言

  在我們平時寫程式碼的時候,免不了會使用到隨機數,特此將幾種隨機的生成總結如下。

二、隨機數生成

  對於隨機數的生成,分為四種情況,假設兩個數為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

三、總結

  對隨機數生成的幾種方法進行了總結,在以後需要的時候直接可以使用,平時多進行積累。謝謝各位園友的觀看~

相關文章