产生一个伪随机的浮点数.
Random ( [最小值 [, 最大值 [, 标志]]] )
最小值 | [可选参数] 随机数的最小值,默认为0. |
最大值 | [可选参数] 随机数的最大值,默认为1. |
标志 | [可选参数] 设为1则返回整数,默认则返回一个浮点数. |
成功: | 返回介于 最小值 及 最大值 之间的一个伪随机数. |
失败: | 返回 0,并把 @error 设为1(参数错误) |
Example1() ; Flip a coin.
Example2() ; Roll a die.
Example3() ; Create a random string of text.
Example4() ; Result of when Min and Max are the same value.
Func Example1()
If Random(0, 1, 1) Then ; Return an integer between 0 and 1.
MsgBox(4096, "", "The side of the coin was: Heads") ; If the random integer was 1 then heads was thrown.
Else
MsgBox(4096, "", "The side of the coin was: Tails") ; If the random integer was 0 then tails was thrown.
EndIf
EndFunc ;==>Example1
Func Example2()
MsgBox(4096, "", "The die landed on number " & Random(1, 6, 1) & ".") ; Return an integer between 1 and 6.
EndFunc ;==>Example2
Func Example3()
Local $sText = ""
For $i = 1 To Random(5, 20, 1) ; Return an integer between 5 and 20 to determine the length of the string.
$sText &= Chr(Random(65, 122, 1)) ; Return an integer between 65 and 122 which represent the ASCII characters between a (lower-case) to Z (upper-case).
Next
MsgBox(4096, "", "The random string of text was: " & $sText) ; Display the string of text.
EndFunc ;==>Example3
Func Example4()
Local $iRandom = Random(10, 10)
If @error Then
MsgBox(4096, "", "An error occurred, due to the fact both values are exactly the same: " & $iRandom)
EndIf
EndFunc ;==>Example4