目錄:
- math.random()
- random類
- 偽隨機
- 如何優化隨機
- 封裝的一個隨機處理工具類
1. math.random()
1.1 介紹
通過math.random()可以獲取隨機數,它返回的是一個[0.0, 1.0)之間的double值。
1
2
3
4
|
private static void testmathrandom() { double random = math.random(); system.out.println( "random = " + random); } |
執行輸出:random = 0.8543235849742018
java中double在32位和64位機器上都是占8個字節,64位,double正數部分和小數部分最多17位有效數字。
如果要獲取int類型的整數,只需要將上面的結果轉行成int類型即可。比如,獲取[0, 100)之間的int整數。方法如下:
1
2
|
double d = math.random(); int i = ( int ) (d* 100 ); |
1.2 實現原理
1
2
3
4
5
6
7
|
private static final class randomnumbergeneratorholder { static final random randomnumbergenerator = new random(); } public static double random() { return randomnumbergeneratorholder.randomnumbergenerator.nextdouble(); } |
- 先獲取一個random對象,在math中是單例模式,唯一的。
- 調用random對象的nextdouble方法返回一個隨機的double數值。
可以看到math.random()方法最終也是調用random類中的方法。
2. random類
2.1 介紹
random類提供了兩個構造器:
1
2
3
4
5
|
public random() { } public random( long seed) { } |
一個是默認的構造器,一個是可以傳入一個隨機種子。
然后通過random對象獲取隨機數,如:
1
|
int r = random.nextint( 100 ); |
2.2 api
1
2
3
4
5
6
7
8
9
|
boolean nextboolean() // 返回一個boolean類型隨機數 void nextbytes( byte [] buf) // 生成隨機字節并將其置于字節數組buf中 double nextdouble() // 返回一個[0.0, 1.0)之間的double類型的隨機數 float nextfloat() // 返回一個[0.0, 1.0) 之間的float類型的隨機數 int nextint() // 返回一個int類型隨機數 int nextint( int n) // 返回一個[0, n)之間的int類型的隨機數 long nextlong() // 返回一個long類型隨機數 synchronized double nextgaussian() // 返回一個double類型的隨機數,它是呈高斯(正常地)分布的 double值,其平均值是0.0,標準偏差是1.0。 synchronized void setseed( long seed) // 使用單個long種子設置此隨機數生成器的種子 |
2.3 例子
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
private static void testrandom(random random) { // 獲取隨機的boolean值 boolean b = random.nextboolean(); system.out.println( "b = " + b); // 獲取隨機的數組buf[] byte [] buf = new byte [ 5 ]; random.nextbytes(buf); system.out.println( "buf = " + arrays.tostring(buf)); // 獲取隨機的double值,范圍[0.0, 1.0) double d = random.nextdouble(); system.out.println( "d = " + d); // 獲取隨機的float值,范圍[0.0, 1.0) float f = random.nextfloat(); system.out.println( "f = " + f); // 獲取隨機的int值 int i0 = random.nextint(); system.out.println( "i without bound = " + i0); // 獲取隨機的[0,100)之間的int值 int i1 = random.nextint( 100 ); system.out.println( "i with bound 100 = " + i1); // 獲取隨機的高斯分布的double值 double gaussian = random.nextgaussian(); system.out.println( "gaussian = " + gaussian); // 獲取隨機的long值 long l = random.nextlong(); system.out.println( "l = " + l); } public static void main(string[] args) { testrandom( new random()); system.out.println( "\n\n" ); testrandom( new random( 1000 )); testrandom( new random( 1000 )); } |
執行輸出:
b = true
buf = [-55, 55, -7, -59, 86]
d = 0.6492428743107401
f = 0.8178623
i without bound = -1462220056
i with bound 100 = 66
gaussian = 0.3794413450456145
l = -5390332732391127434b = true
buf = [47, -38, 53, 63, -72]
d = 0.46028809169559504
f = 0.015927613
i without bound = 169247282
i with bound 100 = 45
gaussian = -0.719106498075259
l = -7363680848376404625b = true
buf = [47, -38, 53, 63, -72]
d = 0.46028809169559504
f = 0.015927613
i without bound = 169247282
i with bound 100 = 45
gaussian = -0.719106498075259
l = -7363680848376404625
可以看到,一次運行過程中,如果種子相同,產生的隨機值也是相同的。
總結一下:
1. 同一個種子,生成n個隨機數,當你設定種子的時候,這n個隨機數是什么已經確定。相同次數生成的隨機數字是完全相同的?! ?br /> 2. 如果用相同的種子創建兩個random 實例,則對每個實例進行相同的方法調用序列,它們將生成并返回相同的數字序列。
2.4 實現原理
先來看看random類構造器和屬性:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
private final atomiclong seed; private static final long multiplier = 0x5deece66dl; private static final long addend = 0xbl; private static final long mask = (1l << 48 ) - 1 ; private static final double double_unit = 0x1 .0p- 53 ; // 1.0 / (1l << 53) private static final atomiclong seeduniquifier = new atomiclong(8682522807148012l); public random() { this (seeduniquifier() ^ system.nanotime()); } private static long seeduniquifier() { for (;;) { long current = seeduniquifier.get(); long next = current * 181783497276652981l; if (seeduniquifier.compareandset(current, next)) return next; } } public random( long seed) { if (getclass() == random. class ) this .seed = new atomiclong(initialscramble(seed)); else { this .seed = new atomiclong(); setseed(seed); } } synchronized public void setseed( long seed) { this .seed.set(initialscramble(seed)); havenextnextgaussian = false ; } |
有兩個構造器,有一個無參,一個可以傳入種子。
種子的作用是什么?
種子就是產生隨機數的第一次使用值,機制是通過一個函數,將這個種子的值轉化為隨機數空間中的某一個點上,并且產生的隨機數均勻的散布在空間中,以后產生的隨機數都與前一個隨機數有關。
無參的通過seeduniquifier() ^ system.nanotime()生成一個種子,里面使用了cas自旋鎖實現。使用system.nanotime()方法來得到一個納秒級的時間量,參與48位種子的構成,然后還進行了一個很變態的運算:不斷乘以181783497276652981l,直到某一次相乘前后結果相同來進一步增大隨機性,這里的nanotime可以算是一個真隨機數,不過有必要提的是,nanotime和我們常用的currenttime方法不同,返回的不是從1970年1月1日到現在的時間,而是一個隨機的數:只用來前后比較計算一個時間段,比如一行代碼的運行時間,數據庫導入的時間等,而不能用來計算今天是哪一天。
不要隨便設置隨機種子,可能運行次數多了會獲取到相同的隨機數,random類自己生成的種子已經能滿足平時的需求了。
以nextint()為例再繼續分析:
1
2
3
4
5
6
7
8
9
|
protected int next( int bits) { long oldseed, nextseed; atomiclong seed = this .seed; do { oldseed = seed.get(); nextseed = (oldseed * multiplier + addend) & mask; } while (!seed.compareandset(oldseed, nextseed)); return ( int )(nextseed >>> ( 48 - bits)); } |
還是通過cas來實現,然后進行位移返回,這塊的算法比較復雜,就不深入研究了。
3. 偽隨機
3.1 什么是偽隨機?
(1) 偽隨機數是看似隨機實質是固定的周期性序列,也就是有規則的隨機。
(2) 只要這個隨機數是由確定算法生成的,那就是偽隨機,只能通過不斷算法優化,使你的隨機數更接近隨機。(隨機這個屬性和算法本身就是矛盾的)
(3) 通過真實隨機事件取得的隨機數才是真隨機數。
3.2 java隨機數產生原理
java的隨機數產生是通過線性同余公式產生的,也就是說通過一個復雜的算法生成的。
3.3 偽隨機數的不安全性
java自帶的隨機數函數是很容易被黑客破解的,因為黑客可以通過獲取一定長度的隨機數序列來推出你的seed,然后就可以預測下一個隨機數。比如eos的dapp競猜游戲,就因為被黑客破解了隨機規律,而盜走了大量的代幣。
4. 如何優化隨機
主要要考慮生成的隨機數不能重復,如果重復則重新生成一個??梢杂脭到M或者set存儲來判斷是否包含重復的隨機數,配合遞歸方式來重新生成一個新的隨機數。
5. 封裝的一個隨機處理工具類
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/u014294681/article/details/86527930