這篇文章主要介紹了Java創建隨機數的方式有哪些的相關知識,內容詳細易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇Java創建隨機數的方式有哪些文章都會有所收獲,下面我們一起來看看吧。
第一次接觸到隨機數還是在c語言里面 使用的是 rand(); 但是重新執行一次的時候會發現,誒,居然和上一次執行的結果是一樣的,因為沒有初始化種子,所以系統用的都是統一的種子這時就會出現每次產生的隨機數都一樣,這時就會使用到 srand();這個隨機數的發生器, 把種子給定當前的時間 即 srand((unsigned) time (&t)); 我c語言常用的隨機數函數如下:
#include <stdio.h> int main(void) { int left,right; printf("請輸入一個數:"); scanf_s("%d%d",&left,&right); //srand((unsigned)time(NULL)); //for(int i = 0; i<10; i++) //printf("%d\n",rand() % one); //改進 for (int i = 0; i < 10; i++) printf("隨機值如下:%d\n", srandNext(left, right)); return 0; } /******************************************************************************** * @Function: srandNext * @Description:Find a random integer from min to max * @Author: caiziqi * @Version: 1.1 * @formal parameter:min : is the minimum value of the value range of this random number function, max: is the maximum value range of this random number function * @Date: 2022-7-26 * @Return : returns a random integer ********************************************************************************/ int srandNext(int max,int min) { //To prevent users from passing parameters incorrectly if (max <= min) { if (max == min) { return max; } int temp = max; max = min; min = temp; } unsigned int static seed = 0; seed++; srand((unsigned)time(NULL) + seed * seed); return rand() % (max - min + 1) + min; }
當然這不是最主要的今天是要講我學到的java中的四種創建隨機數的方法
以下代碼都是以a ~ b 之間 求出一個隨機整數
創建對象
Random ra = new Random(); int right = (Math.max(a,b)) ; int left = (Math.min(a,b); int nu = (ra.nextInt(right) + left );
取值范圍 left 最小值 right 最大值
創建對象
SecureRandom sra = new SecureRandom(); int right = (Math.max(a,b)) ; int left = (Math.min(a,b); int nu = (sra .nextInt(right) + left );
取值范圍 left 最小值 right 最大值
創建對象
ThreadLocalRandom tra = ThreadLocalRandom.current();
求出隨機數的左值和右值
int right = (Math.max(a,b)) ; int left = (Math.min(a,b); int nu = (tra .nextInt(right) + left );
取值范圍 left 最小值 right 最大值
由于 Math 里面的方法是靜態的所以可以直接 類名.方法名 使用
int number = (int)(a + Math.random()*(b-a+1)) //返回一個int類型的參數 所以用 int 接收
范圍 a <= 隨機數 < (b -a +1)
2 <= 隨機數 < (4 -2 +1 )
import java.security.SecureRandom; import java.util.Random; import java.util.Scanner; import java.util.concurrent.ThreadLocalRandom; public class FourWaysOfRandomNumbers { public static void main(String[] args) { //用戶輸入兩個數, 然后程序取這兩個數里面的其中隨機一個數 Scanner input = new Scanner(System.in); System.out.print("請輸入兩個數:"); int a = input.nextInt(); int b = input.nextInt(); // 取值范圍 left 最小值 right 最大值 int right = Math.max(a,b); int left = Math.min(a,b); int nu; //四種生成隨機數的方法 //第一種 Random ra = new Random(); while( true){ nu =(ra.nextInt(right) + left ) ; System.out.println(nu); //找到最大隨機整數 輸出并結束 if(nu == right) { System.out.println(nu); return; } } //第二種 /* SecureRandom sra = new SecureRandom(); while(true){ nu =(sra.nextInt(right) + left ) ; System.out.println(nu); //找到最大隨機整數 輸出并結束 if(nu == right) { System.out.println(nu); return; } }*/ //第三種 在多線程的時候使用 是最佳的; 因為它會為每個線程都 使用不同的種子 /* ThreadLocalRandom tra = ThreadLocalRandom.current(); while(true){ nu =(tra.nextInt(right) + left ) ; System.out.println(nu); //找到最大隨機整數 輸出并結束 if(nu == right) { System.out.println(nu); return; } }*/ /* //第四種 int value = (int)(a + Math.random()*(b-a+1)); System.out.println(value); */ } }
關于“Java創建隨機數的方式有哪些”這篇文章的內容就介紹到這里,感謝各位的閱讀!相信大家對“Java創建隨機數的方式有哪些”知識都有一定的了解,大家如果還想學習更多知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。