這篇來說說java中的隨機數(shù),以及為什么說隨機數(shù)是偽隨機。
目錄:
- math.random()
- random類
- 偽隨機
- 如何優(yōu)化隨機
- 封裝的一個隨機處理工具類
1. math.random()
1.1 介紹
通過math.random()可以獲取隨機數(shù),它返回的是一個[0.0, 1.0)之間的double值。
1
2
3
4
|
private static void testmathrandom() { double random = math.random(); system.out.println( "random = " + random); } |
執(zhí)行輸出:random = 0.8543235849742018
java中double在32位和64位機器上都是占8個字節(jié),64位,double正數(shù)部分和小數(shù)部分最多17位有效數(shù)字。
如果要獲取int類型的整數(shù),只需要將上面的結(jié)果轉(zhuǎn)行成int類型即可。比如,獲取[0, 100)之間的int整數(shù)。方法如下:
1
2
|
double d = math.random(); int i = ( int ) (d* 100 ); |
1.2 實現(xiàn)原理
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中是單例模式,唯一的。
- 調(diào)用random對象的nextdouble方法返回一個隨機的double數(shù)值。
可以看到math.random()方法最終也是調(diào)用random類中的方法。
2. random類
2.1 介紹
random類提供了兩個構(gòu)造器:
1
2
3
4
5
|
public random() { } public random( long seed) { } |
一個是默認的構(gòu)造器,一個是可以傳入一個隨機種子。
然后通過random對象獲取隨機數(shù),如:
1
|
int r = random.nextint( 100 ); |
2.2 api
1
2
3
4
5
6
7
8
9
|
boolean nextboolean() // 返回一個boolean類型隨機數(shù) void nextbytes( byte [] buf) // 生成隨機字節(jié)并將其置于字節(jié)數(shù)組buf中 double nextdouble() // 返回一個[0.0, 1.0)之間的double類型的隨機數(shù) float nextfloat() // 返回一個[0.0, 1.0) 之間的float類型的隨機數(shù) int nextint() // 返回一個int類型隨機數(shù) int nextint( int n) // 返回一個[0, n)之間的int類型的隨機數(shù) long nextlong() // 返回一個long類型隨機數(shù) synchronized double nextgaussian() // 返回一個double類型的隨機數(shù),它是呈高斯(正常地)分布的 double值,其平均值是0.0,標準偏差是1.0。 synchronized void setseed( long seed) // 使用單個long種子設(shè)置此隨機數(shù)生成器的種子 |
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); // 獲取隨機的數(shù)組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 )); } |
執(zhí)行輸出:
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
可以看到,一次運行過程中,如果種子相同,產(chǎn)生的隨機值也是相同的。
總結(jié)一下:
1. 同一個種子,生成n個隨機數(shù),當你設(shè)定種子的時候,這n個隨機數(shù)是什么已經(jīng)確定。相同次數(shù)生成的隨機數(shù)字是完全相同的?! ?br /> 2. 如果用相同的種子創(chuàng)建兩個random 實例,則對每個實例進行相同的方法調(diào)用序列,它們將生成并返回相同的數(shù)字序列。
2.4 實現(xiàn)原理
先來看看random類構(gòu)造器和屬性:
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 ; } |
有兩個構(gòu)造器,有一個無參,一個可以傳入種子。
種子的作用是什么?
種子就是產(chǎn)生隨機數(shù)的第一次使用值,機制是通過一個函數(shù),將這個種子的值轉(zhuǎn)化為隨機數(shù)空間中的某一個點上,并且產(chǎn)生的隨機數(shù)均勻的散布在空間中,以后產(chǎn)生的隨機數(shù)都與前一個隨機數(shù)有關(guān)。
無參的通過seeduniquifier() ^ system.nanotime()生成一個種子,里面使用了cas自旋鎖實現(xiàn)。使用system.nanotime()方法來得到一個納秒級的時間量,參與48位種子的構(gòu)成,然后還進行了一個很變態(tài)的運算:不斷乘以181783497276652981l,直到某一次相乘前后結(jié)果相同來進一步增大隨機性,這里的nanotime可以算是一個真隨機數(shù),不過有必要提的是,nanotime和我們常用的currenttime方法不同,返回的不是從1970年1月1日到現(xiàn)在的時間,而是一個隨機的數(shù):只用來前后比較計算一個時間段,比如一行代碼的運行時間,數(shù)據(jù)庫導(dǎo)入的時間等,而不能用來計算今天是哪一天。
不要隨便設(shè)置隨機種子,可能運行次數(shù)多了會獲取到相同的隨機數(shù),random類自己生成的種子已經(jīng)能滿足平時的需求了。
以nextint()為例再繼續(xù)分析:
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來實現(xiàn),然后進行位移返回,這塊的算法比較復(fù)雜,就不深入研究了。
3. 偽隨機
3.1 什么是偽隨機?
(1) 偽隨機數(shù)是看似隨機實質(zhì)是固定的周期性序列,也就是有規(guī)則的隨機。
(2) 只要這個隨機數(shù)是由確定算法生成的,那就是偽隨機,只能通過不斷算法優(yōu)化,使你的隨機數(shù)更接近隨機。(隨機這個屬性和算法本身就是矛盾的)
(3) 通過真實隨機事件取得的隨機數(shù)才是真隨機數(shù)。
3.2 java隨機數(shù)產(chǎn)生原理
java的隨機數(shù)產(chǎn)生是通過線性同余公式產(chǎn)生的,也就是說通過一個復(fù)雜的算法生成的。
3.3 偽隨機數(shù)的不安全性
java自帶的隨機數(shù)函數(shù)是很容易被黑客破解的,因為黑客可以通過獲取一定長度的隨機數(shù)序列來推出你的seed,然后就可以預(yù)測下一個隨機數(shù)。比如eos的dapp競猜游戲,就因為被黑客破解了隨機規(guī)律,而盜走了大量的代幣。
4. 如何優(yōu)化隨機
主要要考慮生成的隨機數(shù)不能重復(fù),如果重復(fù)則重新生成一個。可以用數(shù)組或者set存儲來判斷是否包含重復(fù)的隨機數(shù),配合遞歸方式來重新生成一個新的隨機數(shù)。
5. 封裝的一個隨機處理工具類
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://blog.csdn.net/u014294681/article/details/86527930