java-Random類
阿新 • • 發佈:2018-12-29
1、Random類的概述和方法使用
* A:Random類的概述
* 此類用於產生隨機數
* 如果用相同的種子建立兩個 Random 例項,則對每個例項進行相同的方法呼叫序列,它們將生成並返回相同的數字序列。
* B:構造方法
* public Random()
* public Random(long seed)
* C:成員方法
* public int nextInt()
* public int nextInt(int n)
例:
1 public class Demo { 2 3 publicstatic void main(String[] args) { 4 Random r = new Random(); 5 6 for(int i = 0; i < 10; i++) { 7 //System.out.println(r.nextInt()); 8 System.out.println(r.nextInt(100)); //生成在0到n範圍內的隨機數,包含0不包含n 9 } 10 11 } 12 13 }