Java生成的隨機數(shù)靠譜嗎?多少次會重復(fù)?
一、前言
本文基于JDK1.8
最近在項目中碰到一個做訂單號的需求,甲方的意思是以字母開頭,后邊跟年份和6位流水號.
我第一反應(yīng)就是流水號用隨機數(shù)生成,突然就想到一個問題,Java的隨機數(shù)真的靠譜嗎?六位數(shù)大概是十萬級別,Java可以保證一萬次不重復(fù)嗎?帶著疑問,我通過三種生成隨機數(shù)的方式做了三個測試.
二、利用Math.random()生成六位隨機數(shù)測試
List<Integer> list=new ArrayList<>();
//記錄計算次數(shù)
Long count=0L;
//記錄循環(huán)次數(shù)
double a=0.0;
//記錄多次計算的總值
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);
}
}
運行結(jié)果如下:

多次運行結(jié)果大概就是在取1100~1300次之間會出現(xiàn)重復(fù).
三、利用new Random().nextInt(999999)生成隨機數(shù)
List<Integer> list=new ArrayList<>();
//記錄計算次數(shù)
Long count=0L;
//記錄循環(huán)次數(shù)
double a=0.0;
//記錄多次計算的總值
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);
}
}
運行結(jié)果:

多次運行結(jié)果大概也是1100~1300次之間重復(fù)
四、利用ThreadLocalRandom.current().nextInt(100000,999999)生成隨機數(shù)
(該方法也是hutool工具RandomUtil.random()的底層實現(xiàn))
public static void main(String[] args) {
List<Integer> list=new ArrayList<>();
//記錄計算次數(shù)
Long count=0L;
//記錄循環(huán)次數(shù)
double a=0.0;
//記錄多次計算的總值
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);
}
}
}
運行結(jié)果:

平均次數(shù)也是1100~1300次之間重復(fù),
五、在2的基礎(chǔ)上做了新的隨機
List<Integer> list=new ArrayList<>();
//記錄計算次數(shù)
Long count=0L;
//記錄循環(huán)次數(shù)
double a=0.0;
//記錄多次計算的總值
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);
}
}
運行結(jié)果

依然也是一千多次就會重復(fù)
由此暫時得出結(jié)論,以上的三(四)種生成隨機數(shù)方法并不能達(dá)到萬次不重復(fù),大概在一千多次時就會出現(xiàn)重復(fù)的問題.
期待一個生成不重復(fù)六位隨機數(shù)的方法
到此這篇關(guān)于Java生成的隨機數(shù)靠譜嗎?會重復(fù)嗎?的文章就介紹到這了,更多相關(guān)Java隨機數(shù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Java生成N個不重復(fù)的隨機數(shù)的三種方法總結(jié)
- 如何通過Java生成一個隨機數(shù)
- Java如何生成隨機數(shù)不了解下嗎
- Java生成隨機數(shù)之Random與ThreadLocalRandom性能比較詳解
- java并發(fā)高的情況下用ThreadLocalRandom來生成隨機數(shù)
- java的三種隨機數(shù)生成方式
- Java中生成隨機數(shù)的4種方式與區(qū)別詳解
- Java實現(xiàn)生成n個不重復(fù)的隨機數(shù)
- JAVA 16位ID生成工具類含16位不重復(fù)的隨機數(shù)數(shù)字+大小寫
- java中生成任意之間數(shù)的隨機數(shù)詳解
- JavaSE API實現(xiàn)生成隨機數(shù)的2種方法(Random類和Math類的Random方法)
相關(guān)文章
如何利用Java使用AOP實現(xiàn)數(shù)據(jù)字典轉(zhuǎn)換
這篇文章主要介紹了如何利用Java使用AOP實現(xiàn)數(shù)據(jù)字典轉(zhuǎn)換,AOP也是我們常說的面向切面編程,AOP在我們開發(fā)過程中應(yīng)用也比較多,在這里我們就基于AOP來實現(xiàn)一個數(shù)據(jù)字典轉(zhuǎn)換的案例2022-06-06
Java中Integer.parseInt和Integer.valueOf區(qū)別小結(jié)
在Java中,Integer.parseInt()和Integer.valueOf()都可以將字符串轉(zhuǎn)換為整數(shù)類型,那么他們有哪些區(qū)別呢,本文就來詳細(xì)的介紹一下2023-09-09
SpringBoot項目中application.yml和bootstrap.yml文件的區(qū)別及說明
`application.yml`和`bootstrap.yml`都是Spring Boot項目中的配置文件,但它們在加載時機、用途、優(yōu)先級、配置來源、適用場景和是否必須存在等方面存在區(qū)別2025-03-03
從零開始搭建springboot+springcloud+mybatis本地項目全過程(圖解)
這篇文章主要介紹了從零開始搭建springboot+springcloud+mybatis本地項目全過程(圖解),本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-01-01
String類型轉(zhuǎn)localDate,date轉(zhuǎn)localDate的實現(xiàn)代碼
這篇文章主要介紹了String類型轉(zhuǎn)localDate,date轉(zhuǎn)localDate的實現(xiàn)代碼,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08

