Java中的隨機(jī)數(shù)Random
一個(gè)用于生成隨機(jī)數(shù)的類
具體用法:
? //創(chuàng)建隨機(jī)數(shù)對(duì)象 ? ? ? ? Random random = new Random(); ? ? ? ? //隨機(jī)產(chǎn)生一個(gè)int類型取值范圍內(nèi)的數(shù)字。 ? ? ? ? int num1 = random.nextInt(); ? ? ? ? System.out.println(num1); ? ? ? ? //產(chǎn)生一個(gè)[0-100]之間的隨機(jī)數(shù) ? ? ? ? int num2 = random.nextInt(101); ? ? ? ? System.out.println(num2);//不包括101
寫一個(gè)不含重復(fù)數(shù)字的隨機(jī)數(shù)組
第一種:
int[] num = new int[5]; ? ? ? ? boolean flag = true; ? ? ? ? Random random = new Random(); ? ? ? ? for (int i = 0; i < num.length; i++) { ? ? ? ? ? ? int a = random.nextInt(5); ? ? ? ? ? ? for (int j = i - 1; j >= 0; j--) {//當(dāng)i == 0 的時(shí)候這一步不執(zhí)行 ? ? ? ? ? ? ? ? if (a == num[j]) { ? ? ? ? ? ? ? ? ? ? flag = false; ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? ? ? if (flag) { ? ? ? ? ? ? ? ? num[i] = a; ? ? ? ? ? ? } else { ? ? ? ? ? ? ? ? i--; ? ? ? ? ? ? ? ? flag = true; ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? ? ? ? ? System.out.println(Arrays.toString(num));
第一種方法的改進(jìn):
public static void main(String[] args) { ? ? ? ? int[] num = new int[5]; ? ? ? ? Random random = new Random(); ? ? ? ? int index = 0; ? ? ? ? while (index < num.length) { ? ? ? ? ? ? int a = random.nextInt(5); ? ? ? ? ? ? if (contains(num, index, a)) { //把判斷有沒有變成了一個(gè)方法 ? ? ? ? ? ? ? ? num[index++] = a; ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? System.out.println(Arrays.toString(num)); ? ? } ? ? public static boolean contains(int[] a, int index, int temp) { ? ? ? ? for (int i = index - 1; i >= 0; i--) { ? ? ? ? ? ? if (temp == a[i]) { ? ? ? ? ? ? ? ? return false; ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? return true; ? ? }
第二種:
不推薦這種方法,雖然使用了Arrays
自帶的方法 ,但是這個(gè)方法的除最后一個(gè)元素以外的元素都是排好序的
這種隨機(jī)有點(diǎn)不嚴(yán)謹(jǐn)
public static void main(String[] args) { ? ? ? ? int[] a = new int[5]; ? ? ? ? Random random = new Random(); ? ? ? ? int index = -1; ? ? ? ? while(index < a.length -1){ ? ? ? ? ? ? int b = random.nextInt(5); ? ? ? ? ? ? if(contains(a,b,index)){ ? ? ? ? ? ? ? ? a[++index] = b; ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? System.out.println(Arrays.toString(a)); ? ? } ? ? public static boolean contains(int[] a , int b , int index){ ? ? ? ? if (index < 0){ ? ? ? ? ? ? return true; ? ? ? ? } ? ? ? ? ? ? Arrays.sort(a,0,index + 1);//下標(biāo)為[0,index+1)的數(shù)組排序 不包含index+1 ? ? ? ? return Arrays.binarySearch(a,0,index + 1,b) < 0;//二分法查找下標(biāo)為[0,index+1)范圍內(nèi)是否包含b ? ? ? }
到此這篇關(guān)于Java中的隨機(jī)數(shù)Random的文章就介紹到這了,更多相關(guān)隨機(jī)數(shù)Random內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot + Mybatis Plus 整合 Redis的
文章詳細(xì)介紹了Redis在用戶管理系統(tǒng)中的應(yīng)用,包括用戶信息緩存、Token存儲(chǔ)、接口限流、重復(fù)提交攔截和熱點(diǎn)數(shù)據(jù)預(yù)加載等場(chǎng)景,并提供了具體的實(shí)現(xiàn)方案和步驟,感興趣的朋友一起看看吧2025-03-03如何使用BigDecimal實(shí)現(xiàn)Java開發(fā)商業(yè)計(jì)算
這篇文章主要介紹了如何使用BigDecimal實(shí)現(xiàn)Java開發(fā)商業(yè)計(jì)算,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09IDEA中設(shè)置代碼自動(dòng)提示為Alt+/的具體做法
很多公司都強(qiáng)制性要求使用Intellij?IDEA,其實(shí)Intellij?IDEA也確實(shí)很好用,但是一下子從Eclipse跳轉(zhuǎn)到Intellij?IDEA轉(zhuǎn)也是需要一段時(shí)間的,為了迎合之前的習(xí)慣,就需要在Intellij?IDEA中改變一些設(shè)置,如代碼自動(dòng)生成,本文給大家分享設(shè)置方法,感興趣的朋友一起看看吧2023-01-01Hibernate實(shí)現(xiàn)悲觀鎖和樂觀鎖代碼介紹
這篇文章主要介紹了Hibernate實(shí)現(xiàn)悲觀鎖和樂觀鎖的有關(guān)內(nèi)容,涉及hibernate的隔離機(jī)制,以及實(shí)現(xiàn)悲觀鎖和樂觀鎖的代碼實(shí)現(xiàn),需要的朋友可以了解下。2017-09-09SpringMVC整合,出現(xiàn)注解沒有起作用的情況處理
這篇文章主要介紹了SpringMVC整合,出現(xiàn)注解沒有起作用的情況及處理,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。2023-05-05springboot中RabbitMQ死信隊(duì)列的實(shí)現(xiàn)示例
死信隊(duì)列是一種特殊的消息隊(duì)列,用來存儲(chǔ)無法被正常消費(fèi)的消息,常被用來實(shí)現(xiàn)延遲處理,異常消息處理等,本文主要介紹了springboot中RabbitMQ死信隊(duì)列的實(shí)現(xiàn)示例,感興趣的可以了解一下2024-01-01Java字節(jié)緩存流的構(gòu)造方法之文件IO流
這篇文章主要介紹了Java字節(jié)緩存流的構(gòu)造方法之文件IO流,同時(shí)也介紹了字符流中的一些相關(guān)的內(nèi)容,并且通過大量的案例供大家理解。最后通過一些經(jīng)典的案例幫助大家對(duì)前面所學(xué)的知識(shí)做了一個(gè)綜合的應(yīng)用,需要的朋友可以參考一下2022-04-04