Java生成的隨機(jī)數(shù)靠譜嗎?多少次會(huì)重復(fù)?
一、前言
本文基于JDK1.8
最近在項(xiàng)目中碰到一個(gè)做訂單號(hào)的需求,甲方的意思是以字母開頭,后邊跟年份和6位流水號(hào).
我第一反應(yīng)就是流水號(hào)用隨機(jī)數(shù)生成,突然就想到一個(gè)問題,Java的隨機(jī)數(shù)真的靠譜嗎?六位數(shù)大概是十萬級(jí)別,Java可以保證一萬次不重復(fù)嗎?帶著疑問,我通過三種生成隨機(jī)數(shù)的方式做了三個(gè)測(cè)試.
二、利用Math.random()生成六位隨機(jī)數(shù)測(cè)試
List<Integer> list=new ArrayList<>(); //記錄計(jì)算次數(shù) Long count=0L; //記錄循環(huán)次數(shù) double a=0.0; //記錄多次計(jì)算的總值 Long sum=0L; while (true){ count++; Integer i = (int)((Math.random()*9+1)*100000); System.out.println("第"+count+"次,i為:"+i); if (list.contains(i)){ a++; sum+=count; count=0L; list.clear(); if (a>=1000){ System.out.println("平均:"+(double)sum/a); break; } }else{ list.add(i); } }
運(yùn)行結(jié)果如下:
多次運(yùn)行結(jié)果大概就是在取1100~1300次之間會(huì)出現(xiàn)重復(fù).
三、利用new Random().nextInt(999999)生成隨機(jī)數(shù)
List<Integer> list=new ArrayList<>(); //記錄計(jì)算次數(shù) Long count=0L; //記錄循環(huán)次數(shù) double a=0.0; //記錄多次計(jì)算的總值 Long sum=0L; while (true){ count++; Integer i = new Random().nextInt(999999); while (i<100000){ i=i*10; } System.out.println("第"+count+"次,i為:"+i); if (list.contains(i)){ a++; sum+=count; count=0L; list.clear(); if (a>=1000){ System.out.println("平均:"+(double)sum/a); break; } }else{ list.add(i); } }
運(yùn)行結(jié)果:
多次運(yùn)行結(jié)果大概也是1100~1300次之間重復(fù)
四、利用ThreadLocalRandom.current().nextInt(100000,999999)生成隨機(jī)數(shù)
(該方法也是hutool工具RandomUtil.random()的底層實(shí)現(xiàn))
public static void main(String[] args) { List<Integer> list=new ArrayList<>(); //記錄計(jì)算次數(shù) Long count=0L; //記錄循環(huán)次數(shù) double a=0.0; //記錄多次計(jì)算的總值 Long sum=0L; while (true){ count++; Integer i = ThreadLocalRandom.current().nextInt(100000,999999); System.out.println("第"+count+"次,i為:"+i); if (list.contains(i)){ a++; sum+=count; count=0L; list.clear(); if (a>=1000){ System.out.println("平均:"+(double)sum/a); break; } }else{ list.add(i); } } }
運(yùn)行結(jié)果:
平均次數(shù)也是1100~1300次之間重復(fù),
五、在2的基礎(chǔ)上做了新的隨機(jī)
List<Integer> list=new ArrayList<>(); //記錄計(jì)算次數(shù) Long count=0L; //記錄循環(huán)次數(shù) double a=0.0; //記錄多次計(jì)算的總值 Long sum=0L; while (true){ count++; Integer i = new Random().nextInt(999999); while (i<100000){ i=Integer.parseInt(String.valueOf(i)+String.valueOf(new Random().nextInt(10))); } System.out.println("第"+count+"次,i為:"+i); if (list.contains(i)){ a++; sum+=count; count=0L; list.clear(); if (a>=1000){ System.out.println("平均:"+(double)sum/a); break; } }else{ list.add(i); } }
運(yùn)行結(jié)果
依然也是一千多次就會(huì)重復(fù)
由此暫時(shí)得出結(jié)論,以上的三(四)種生成隨機(jī)數(shù)方法并不能達(dá)到萬次不重復(fù),大概在一千多次時(shí)就會(huì)出現(xiàn)重復(fù)的問題.
期待一個(gè)生成不重復(fù)六位隨機(jī)數(shù)的方法
到此這篇關(guān)于Java生成的隨機(jī)數(shù)靠譜嗎?會(huì)重復(fù)嗎?的文章就介紹到這了,更多相關(guān)Java隨機(jī)數(shù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Java生成N個(gè)不重復(fù)的隨機(jī)數(shù)的三種方法總結(jié)
- 如何通過Java生成一個(gè)隨機(jī)數(shù)
- Java如何生成隨機(jī)數(shù)不了解下嗎
- Java生成隨機(jī)數(shù)之Random與ThreadLocalRandom性能比較詳解
- java并發(fā)高的情況下用ThreadLocalRandom來生成隨機(jī)數(shù)
- java的三種隨機(jī)數(shù)生成方式
- Java中生成隨機(jī)數(shù)的4種方式與區(qū)別詳解
- Java實(shí)現(xiàn)生成n個(gè)不重復(fù)的隨機(jī)數(shù)
- JAVA 16位ID生成工具類含16位不重復(fù)的隨機(jī)數(shù)數(shù)字+大小寫
- java中生成任意之間數(shù)的隨機(jī)數(shù)詳解
- JavaSE API實(shí)現(xiàn)生成隨機(jī)數(shù)的2種方法(Random類和Math類的Random方法)
相關(guān)文章
Springboot自定義注解&傳參&簡(jiǎn)單應(yīng)用方式
SpringBoot框架中,通過自定義注解結(jié)合AOP可以實(shí)現(xiàn)功能如日志記錄與耗時(shí)統(tǒng)計(jì),首先創(chuàng)建LogController和TimeConsuming注解,并為LogController定義參數(shù),然后,在目標(biāo)方法上應(yīng)用這些注解,最后,使用AspectJ的AOP功能,通過切點(diǎn)表達(dá)式定位這些注解2024-10-10java實(shí)現(xiàn)相同屬性名稱及相似類型的pojo、dto、vo等互轉(zhuǎn)操作
這篇文章主要介紹了java實(shí)現(xiàn)相同屬性名稱及相似類型的pojo、dto、vo等互轉(zhuǎn)操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-08-08SpringBoot、mybatis返回樹結(jié)構(gòu)的數(shù)據(jù)實(shí)現(xiàn)
本文主要介紹了SpringBoot、mybatis返回樹結(jié)構(gòu)的數(shù)據(jù)實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04