BlitzMax/模組/數學/隨機數
外觀
隨機模組包含用於生成隨機數的命令。
生成的數字並不真正隨機,因為如果沒有特殊的硬體支援,軟體演算法無法生成“真實”的隨機數。
相反,演算法會生成看起來像是隨機的數值。實際上,每個生成的數值實際上都依賴於上一個生成的數值。
可以使用 SeedRnd 命令設定隨機數生成器的“狀態”。一種常見的做法是在程式啟動時使用系統時間作為隨機數生成器的種子,例如
SeedRnd MilliSecs()
這可以確保隨機數生成器在每次執行程式時不會處於相同狀態,這會導致它生成相同的隨機數序列。
函式 RndFloat#()
描述: 生成隨機浮點數
返回值: 範圍在 0(包含)到 1(不包含)之間的隨機浮點數
示例:
' RndFloat.bmx
' Two players take turns shooting at a target. The first hit wins.
' Player 1 hits 30% of the time, player 2 hits 40%.
' What is the probability that player 1 wins?
Function winner() ' play game once, return winner 1 or 2
Repeat
If RndFloat() < 0.3 Then Return 1
If RndFloat() < 0.4 Then Return 2
Forever
End Function
Local count[3]
trials = 1000000
For n = 1 to trials
count[ winner() ] :+ 1
Next
Print "Estimated probability = " + ( Float( count[1] ) / Float( trials ) )
Print
Print " Exact probability = " + ( 15.0 / 29.0 )
Input ; End
函式 RndDouble!()
描述: 生成隨機雙精度浮點數
返回值: 範圍在 0(包含)到 1(不包含)之間的隨機雙精度浮點數
示例:
' RndDouble.bmx
' Two players take turns shooting at a target. The first hit wins.
' Player 1 hits 30% of the time, player 2 hits 40%.
' What is the probability that player 1 wins?
Function winner() ' play game once, return winner 1 or 2
Repeat
If RndDouble() < 0.3 Then Return 1
If RndDouble() < 0.4 Then Return 2
Forever
End Function
Local count[3]
trials = 1000000
For n = 1 to trials
count[ winner() ] :+ 1
Next
Print "Estimated probability = " + ( Double( count[1] ) / Double( trials ) )
Print
Print " Exact probability = " + ( 15.0 / 29.0 )
Input ; End
函式 Rnd!( min_value!=0,max_value!=1 )
描述: 生成隨機雙精度浮點數
返回值: 範圍在 min(包含)到 max(不包含)之間的隨機雙精度浮點數
資訊: 可選引數允許您以 3 種方式使用 Rnd
| 格式 | 結果 |
| &Rnd() | 範圍在 0(包含)到 1(不包含)之間的隨機雙精度浮點數 |
| &Rnd(_x_) | 範圍在 0(包含)到 n(不包含)之間的隨機雙精度浮點數 |
| &Rnd(_x,y_) | 範圍在 x(包含)到 y(不包含)之間的隨機雙精度浮點數 |
示例:
' Rnd.bmx
' Use Rnd() to estimate area inside the unit circle x^2 + y^2 = 1.
totalpoints = 1000000
For n = 1 to totalpoints
x! = Rnd( -1.0, 1.0 ) ' Generate random point in 2 by 2 square.
y! = Rnd( -1.0, 1.0 )
If x*x + y*y < 1.0 Then pinpoints{{typo help inline|reason=similar to pinpoints|date=September 2022}} :+ 1 ' point is inside the circle
Next
' Note: Ratio of areas circle/square is exactly Pi/4.
Print "Estimated area = " + ( 4.0 * Double(inpoints)/Double(totalpoints) )
Print
Print " Exact area = " + Pi ' 4 * Pi/4, compare with estimate
Input ; End
函式 Rand( min_value,max_value=1 )
描述: 生成隨機整數
返回值: 範圍在 min(包含)到 max(包含)之間的隨機整數
資訊: 可選引數允許您以 2 種方式使用 Rand
| 格式 | 結果 |
| &Rand(x) | 範圍在 1 到 x(包含)之間的隨機整數 |
| &Rand(x,y) | 範圍在 x 到 y(包含)之間的隨機整數 |
示例:
' Rand.bmx
' Toss a pair of dice. Result is in the range 1+1 to 6+6.
' Count how many times each result appears.
Local count[13]
For n = 1 To 3600
toss = Rand(1,6) + Rand(1,6)
count[toss] :+ 1
Next
For toss = 2 To 12
Print LSet(toss, 5)+count[toss]
Next
函式 SeedRnd( seed )
描述: 設定隨機數生成器種子
示例:
' RndSeed.bmx and SeedRnd.bmx ( one example for both ) ' Get/Set random number seed. SeedRnd MilliSecs() seed=RndSeed() Print "Initial seed="+seed For k=1 To 10 Print Rand(10) Next Print "Restoring seed" SeedRnd seed For k=1 To 10 Print Rand(10) Next
函式 RndSeed()
描述: 獲取隨機數生成器種子
返回值: 當前隨機數生成器種子
資訊: 與 SeedRnd 結合使用,RndSeed 允許您重現隨機數序列。
示例:
' RndSeed.bmx and SeedRnd.bmx ( one example for both ) ' Get/Set random number seed. SeedRnd MilliSecs() seed=RndSeed() Print "Initial seed="+seed For k=1 To 10 Print Rand(10) Next Print "Restoring seed" SeedRnd seed For k=1 To 10 Print Rand(10) Next