JDK_執行個體(java如何產生隨機數)

來源:互聯網
上載者:User

下文來自:

http://scliu123.iteye.com/blog/506387

--------------------------
package book.number;

import java.util.Random;
/**
 * Java工具 + 生產力類庫中的類java.util.Random提供了產生各種類型隨機數的方法。
 * 它可以產生int、long、float、double以及Goussian等類型的隨機數。
 * java.lang.Math中的方法random()只產生double型的隨機數。
 */
public class RandomNumber {

    public static void main(String[] args) {

        // 使用java.lang.Math的random方法產生隨機數
        System.out.println("Math.random(): " + Math.random());

        // 使用不帶參數的構造方法構造java.util.Random對象
        System.out.println("使用不帶參數的構造方法構造的Random對象:");
        Random rd1 = new Random();
        // 產生各種類型的隨機數
        // 按均勻分布產生整數
        System.out.println("int: " + rd1.nextInt());
        // 按均勻分布產生長整數
        System.out.println("long: " + rd1.nextLong());
        // 按均勻分布產生大於等於0,小於1的float數[0, 1)
        System.out.println("float: " + rd1.nextFloat());
        // 按均勻分布產生[0, 1)範圍的double數
        System.out.println("double: " + rd1.nextDouble());
        // 按常態分佈產生隨機數
        System.out.println("Gaussian: " + rd1.nextGaussian());

        // 產生一系列隨機數
        System.out.print("隨機整數序列:");
        for (int i = 0; i < 5; i++) {
            System.out.print(rd1.nextInt() + "  ");
        }
        System.out.println();

        // 指定隨機數產生的範圍
        System.out.print("[0,10)範圍內隨機整數序列: ");
        for (int i = 0; i < 10; i++) {
            // Random的nextInt(int n)方法返回一個[0, n)範圍內的隨機數
            System.out.print(rd1.nextInt(10) + "  ");
        }
        System.out.println();
        System.out.print("[5,23)範圍內隨機整數序列: ");
        for (int i = 0; i < 10; i++) {
            // 因為nextInt(int n)方法的範圍是從0開始的,
            // 所以需要把區間[5,28)轉換成5 + [0, 23)。
            System.out.print(5 + rd1.nextInt(23) + "  ");
        }
        System.out.println();
        System.out.print("利用nextFloat()產生[0,99)範圍內的隨機整數序列: ");
        for (int i = 0; i < 10; i++) {
            System.out.print((int) (rd1.nextFloat() * 100) + "  ");
        }
        System.out.println();
        System.out.println();

        // 使用帶參數的構造方法構造Random對象
        // 建構函式的參數是long類型,是產生隨機數的種子。
        System.out.println("使用帶參數的構造方法構造的Random對象:");
        Random ran2 = new Random(10);
        // 對於種子相同的Random對象,產生的隨機數序列是一樣的。
        System.out.println("使用種子為10的Random對象產生[0,10)內隨機整數序列: ");
        for (int i = 0; i < 10; i++) {
            System.out.print(ran2.nextInt(10) + "  ");
        }
        System.out.println();
        Random ran3 = new Random(10);
        System.out.println("使用另一個種子為10的Random對象產生[0,10)內隨機整數序列: ");
        for (int i = 0; i < 10; i++) {
            System.out.print(ran3.nextInt(10) + "  ");
        }
        System.out.println();
        // ran2和ran3產生的隨機數序列是一樣的,如果使用兩個沒帶參數建構函式產生的Random對象,
        // 則不會出現這種情況,這是因為在沒帶參數建構函式產生的Random對象的種子預設是當前系統時間的毫秒數。

        // 另外,直接使用Random無法避免產生重複的數字,如果需要產生不重複的隨機數序列,需要藉助數組和集合類
        //本書第4章將給出解決方案。
    }
}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.