在寫程式的時候常常會需要進行一些隨機的動作,來增加程式的娛樂性和複雜性。在 Java 程式語言中,可以使用 Math 類別的 random 方法或是 Random 物件來達到取得隨機數字的目的。取得隨機數字之後還需要撰寫一些程式來使用這個隨機數,這類程式常會頻繁地重複撰寫,十分麻煩。為了精簡程式,MagicRandom 實作好了一些常用的隨機資料程式,只要使用一個靜態方法就可以完成大部分的事。
下載 MagicRandom
MagicRandom 的應用
以下列舉一些亂數的例子,並分別使用 Math.random()、Random 物件和 MagicRandom 來實作同樣的效果。MagicRandom 相對其他兩者來說要容易使用得多。
限定範圍隨機產生一個整數(int)資料
如果要隨便在一定範圍內產生出一個整數(int)資料,程式實作方式如下。
Math.random()
Math.random() 只會傳回一個 0~1.0(包含 0,但不包含 1.0)的浮點數,所以還需要經過一些運算來使它可以產生某個範圍內的數。首先要將它乘以某個倍數來延展,擴充或縮減它可以隨機產生出來的數字區間大小,接著要加上某數讓它位移到正確的數字範圍內。
假設我們要從 min~max 整數範圍中隨機產生一個整數,共有「max - min + 1」個可能會出現的整數,程式可以這樣寫:
int i = (int) (Math.random() * (max - min + 1) + min);
Random 物件
Random 物件的 ints 方法可以產生 IntStream 物件,使用 IntStream 物件的 findFirst 方法可以取得第一個隨機產生出來的整數。
Random random = new Random();
int i = random.ints(min, max + 1).findFirst().getAsInt();
MagicRandom
直接使用 MagicRandom 類別的 randomInteger 方法。
int i = MagicRandom.randomInteger(min, max);
不限範圍隨機產生一個短整數(short)資料
如果要隨便產生出一個短整數(short)資料,程式實作方式如下。
Math.random()
short s = (short) (Math.random() * (Short.MAX_VALUE - Short.MIN_VALUE + 1) + Short.MIN_VALUE);
Random 物件
Random random = new Random();
short s = (short)random.ints(Short.MIN_VALUE, Short.MAX_VALUE + 1).findFirst().getAsInt();
MagicRandom
直接使用 MagicRandom 類別的 randomShort 方法。
short s = MagicRandom.randomShort();
限定範圍隨機產生一組可重複的整數(int)資料
Math.random()
int[] array = new int[length];
for (int a = 0; a < array.length; ++a) {
array[a] = (int) (Math.random() * (max - min + 1) + min);
}
Random 物件
Random 物件的 ints 方法可以產生 IntStream 物件,使用 IntStream 物件的 toArray 方法可以將 IntStream 物件轉成 int 陣列物件。
int[] array = random.ints(length, min, max + 1).toArray();
MagicRandom
直接使用 MagicRandom 類別的 randomIntegerArray 方法。
int[] array = MagicRandom.randomIntegerArray(min, max, length, false);
限定範圍隨機產生一組不重複的整數(int)資料
這個例子好像有點難以理解,其實就像是樂透開獎。就以 1~48 號選出 6 個數字當作例子,程式實作方式如下。
Math.random()
由於需要抽選不重複的亂數,因此亂數在產生之後還需要判斷是否已經抽選過,若有抽選過就再重新抽選一次。
int[] array = new int[6];
for (int a = 0; a < array.length; ++a) {
int i = 0;
pick:
while (i == 0) {
i = (int) (Math.random() * 48 + 1);
for (int b = 0; b < a; ++b) {
if (array[b] == i) {
i = 0;
continue pick;
}
}
array[a] = i;
}
}
Random 物件
Random 物件的 ints 方法可以產生 IntStream 物件,但不能直接使用 IntStream 物件的 toArray 方法來直接取得 int 陣列物件,因為元素可能會重複。所以也還需要判斷是否已經抽選過,若有抽選過就再重新抽選一次。
int[] array = new int[6];
for (int a = 0; a < array.length; ++a) {
int i = 0;
pick:
while (i == 0) {
i = random.ints(1, 49).findFirst().getAsInt();
for (int b = 0; b < a; ++b) {
if (array[b] == i) {
i = 0;
continue pick;
}
}
array[a] = i;
}
}
MagicRandom
直接使用 MagicRandom 類別的 randomIntegerArray 方法。
int[] array = MagicRandom.randomIntegerArray(min, max, length, true);
完全隨機抽取某陣列中的一個元素
Math.random()
先隨機選出索引值,再用索引值來取得元素。
int i = (int) (Math.random() * array.length);
int element = array[i];
Random 物件
同樣也是先隨機選出索引值,再用索引值來取得元素。Random 物件的 nextInt(bound) 方法可以隨機產生大於等於 0 且小於 bound 的整數。
int i = random.nextInt(array.length);
int element = array[i];
MagicRandom
直接使用 MagicRandom 類別的 singlePickFromArray 方法。
int element = MagicRandom.singlePickFromArray(array);
依權重隨機抽取某陣列中的一個元素
這個例子好像有點難以理解,其實就像是一般的抽獎活動,愈大獎抽到的機率愈少。假設有個抽獎活動共有 5 種獎項,愈前面的獎項價值愈高,所以抽到的機會也愈少。若第 1 種獎項抽到的機率為 2%,第 2 種獎項抽到的機率為 5%,第 3 種獎項抽到的機率為 10%,第 4 種獎項抽到的機率為 33%,第 5 種獎項抽到的機率為 50%。若要從 prizes 這個字串陣列中抽出獎項:
String[] prizes = new String[]{"頭獎", "二獎", "三獎", "四獎", "五獎"};
程式實作方式如下。
Math.random()
從 0~99 抽出一個數字,判斷這個數字落在哪個範圍內來決定對應到的獎項。
String prize;
int i = (int) (Math.random() * 100);
if (i >= 0 && i <= 1) {
prize = prizes[0];
} else if (i >= 2 && i <= 6) {
prize = prizes[1];
} else if (i >= 7 && i <= 16) {
prize = prizes[2];
} else if (i >= 17 && i <= 49) {
prize = prizes[3];
} else {
prize = prizes[4];
}
Random 物件
同樣也是從 0~99 抽出一個數字,判斷這個數字落在哪個範圍內來決定對應到的獎項。
String prize;
int i = random.nextInt(100);
if (i >= 0 && i <= 1) {
prize = prizes[0];
} else if (i >= 2 && i <= 6) {
prize = prizes[1];
} else if (i >= 7 && i <= 16) {
prize = prizes[2];
} else if (i >= 17 && i <= 49) {
prize = prizes[3];
} else {
prize = prizes[4];
}
MagicRandom
直接使用 MagicRandom 類別的 singlePickFromArray 方法,利用 singlePickFromArray 方法的參數來設定元素的權重。
String prize = MagicRandom.singlePickFromArray(prizes, 2, 5, 10, 33, 50);
