生成隨機數本身具有良好的實用價值,並且通過使用函數實現隨機數可以證明是非常有用的。 Java在其語言中已經將整個庫專用於隨機數,從而看到了它在日常編程中的重要性。本文討論了nextInt()。
- java.util.Random.nextInt():nextInt()用於從此隨機數生成器的序列中獲取下一個隨機整數值。
Declaration: public int nextInt() 參數: NA 返回值: The method call returns the next integer number from the sequence Exception: NA
以下示例顯示了java.util.Random.nextInt()的用法
// Java code to demonstrate the working // of nextInt() import java.util.*; public class NextInt1 { public static void main(String[] args) { // create random object Random ran = new Random(); // generating integer int nxt = ran.nextInt(); // Printing the random Number System.out.println ("The Randomly generated integer is:" + nxt); } }
輸出:
The Randomly generated integer is:-2052834321
-
java.util.Random.nextInt(int n):nextInt(int n)用於獲取介於0(含)和此參數(n)中所傳遞的數字(不包括)之間的隨機數。
Declaration: public int nextInt(int n) 參數: n: This is the bound on the random number to be returned. Must be positive. 返回值: Returns a random number. between 0 (inclusive) and n (exclusive). Exception: IllegalArgumentException: This is thrown if n is not positive.
下麵的示例顯示java.util.Random.nextInt(int n)的用法
// Java code to demonstrate the working // of nextInt(n) import java.util.*; public class NextInt2 { public static void main(String args[]) { // create random object Random ran = new Random(); // Print next int value // Returns number between 0-9 int nxt = ran.nextInt(10); // Printing the random number // between 0 and 9 System.out.println ("Random number between 0 and 10 is:" + nxt); } }
輸出:
Random number between 0 and 9 is:4
異常
IllegalArgumentException:當傳遞的參數不是正數時,會發生這種情況。
一個示例說明當n不是正數時生成的異常:
// Java code to demonstrate the Exception
// of nextInt(n)
import java.util.*;
public class NextInt2 {
public static void main(String[] args)
{
// create random object
Random ran = new Random();
// generating number between 0 and -12345
// Raises Runtime error, as n is negative.
int nxt = ran.nextInt(-12345);
System.out.println
("Generated Random number is:" + nxt);
}
}
運行時錯誤:
Exception in thread "main" java.lang.IllegalArgumentException:bound must be positive at java.util.Random.nextInt(Random.java:388) at NextInt2.main(NextInt2.java:14)
Practical Applications
產生隨機數有許多應用,無論是彩票,賭博還是小規模遊戲。下麵展示了一個小型骰子遊戲,其中2名玩家擲出6分骰子,第一人獲得30分,獲勝。
// Java code to demonstrate the Application
// of nextInt(n)
import java.util.*;
public class NextIntAppli {
public static void main(String[] args)
{
int sum = 0, sum1 = 0, count = 0, count1 = 0;
int turn = 0;
// creating random object
Random ran = new Random();
int flag = 0;
while (true) {
if (turn % 2 == 0) {
int p1 = ran.nextInt(6);
sum += p1;
System.out.printf
("Player 1 after turn %d is:%d\n", turn, sum);
}
else {
int p2 = ran.nextInt(6);
sum1 += p2;
System.out.printf
("Player 2 after turn %d is:%d\n", turn, sum1);
}
if (sum >= 30) {
flag = 1;
break;
}
if (sum1 >= 30) {
flag = 2;
break;
}
turn++;
}
if (flag == 1)
System.out.println("\nPlayer 1 WON!!");
else
System.out.println("\nPlayer 2 WON!!");
}
}
輸出:
Player 1 after turn 0 is:0 Player 2 after turn 1 is:4 Player 1 after turn 2 is:2 Player 2 after turn 3 is:9 Player 1 after turn 4 is:5 Player 2 after turn 5 is:9 Player 1 after turn 6 is:6 Player 2 after turn 7 is:14 Player 1 after turn 8 is:8 Player 2 after turn 9 is:18 Player 1 after turn 10 is:12 Player 2 after turn 11 is:21 Player 1 after turn 12 is:13 Player 2 after turn 13 is:26 Player 1 after turn 14 is:18 Player 2 after turn 15 is:29 Player 1 after turn 16 is:18 Player 2 after turn 17 is:34 Player 2 WON!!
相關用法
- Java Java.util.function.IntPredicate用法及代碼示例
- Java Java lang.Long.numberOfTrailingZeros()用法及代碼示例
- Java Java.util.concurrent.Phaser用法及代碼示例
- Java Java lang.Long.reverse()用法及代碼示例
- Java Java lang.Long.lowestOneBit()用法及代碼示例
- Java Java lang.Long.byteValue()用法及代碼示例
- Java Java lang.Long.highestOneBit()用法及代碼示例
- Java Java.util.function.LongPredicate用法及代碼示例
- Java Java.util.function.DoublePredicate用法及代碼示例
- Java Java.util.function.BiPredicate用法及代碼示例
- Java Java.util.concurrent.RecursiveAction用法及代碼示例
- Java Java lang.Long.numberOfLeadingZeros()用法及代碼示例
注:本文由純淨天空篩選整理自suman_709大神的英文原創作品 Java.util.Random.nextInt() in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。