Rnd Function

傳回 0 到 1 之間的隨機數。

語法


Rnd [(Expression)]

傳回值類型

Double

參數:

Expression: Has no effect, is ignored if provided.

The Rnd function returns decimal fractions ranging from 0 (included) to 1 (excluded) according to a uniform distribution. It uses the Mersenne Twister 19937 random-number generator. To generate random integers in a given range, use a formula like in the example below. A Randomize statement with a defined seed value can be used beforehand, if a predictable sequence of numbers is desired.

錯誤代碼:

5 無效的程序呼叫

示例:


Sub ExampleRandomSelect
Dim iVar As Integer
    iVar = Int((15 * Rnd) -2)
    Select Case iVar
        Case 1 To 5
            Print "Number from 1 to 5"
        Case 6, 7, 8
            Print "Number from 6 to 8"
        Case Is > 8 And iVar < 11
            Print "Greater than 8"
        Case Else
            Print "Outside range 1 to 10"
    End Select
End Sub

Please support us!