Random類的概述和方法

Random類的概述和方法

  • A:Random類的概述
    • 此類用於產生隨機數如果用相同的種子創建兩個 Random 實例。
    • 則對每個實例進行相同的方法調用序列,它們將生成並返回相同的數字序列。
  • B:構造方法
    • public Random()
    • public Random(long seed)
  • C:成員方法
    • public int nextInt()
    • public int nextInt(int n)(重點掌握)
package com.heima.otherclass;

import java.util.Random;
public class Demo02_Random {

	public static void main(String[] args) {
		Random r = new Random();
		/*int x = r.nextInt();
		
		System.out.println(x);*/
		
		for(int i = 0; i < 10; i++) {
			//System.out.println(r.nextInt());
			System.out.println(r.nextInt(100));			//要求掌握,生成在0到n範圍內的隨機數,包含0不包含n
		}
		
		/*
		 * -1244746321
			1060493871
			
			-1244746321
			1060493871
		 */
		運用種子數獲取隨機數的值是固定的。
		Random r2 = new Random(1001);
		
		int a = r2.nextInt();
		int b = r2.nextInt();
		
		System.out.println(a);
		System.out.println(b);
	}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章