VBMath.Rnd 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
回傳一個類型 Single為 的隨機數字。
多載
| 名稱 | Description |
|---|---|
| Rnd() |
回傳一個類型 Single為 的隨機數字。 |
| Rnd(Single) |
回傳一個類型 Single為 的隨機數字。 |
Rnd()
回傳一個類型 Single為 的隨機數字。
public:
static float Rnd();
public static float Rnd();
static member Rnd : unit -> single
Public Function Rnd () As Single
傳回
序列中的下一個隨機數字。
範例
此範例使用函 Rnd 數生成一個範圍介於1到6的隨機整數值。
' Initialize the random-number generator.
Randomize()
' Generate random value between 1 and 6.
Dim value As Integer = CInt(Int((6 * Rnd()) + 1))
備註
該 Rnd 函數回傳的值小於1,但大於或等於零。
在呼叫 Rnd之前,使用 Randomize 無參數的語句,以基於系統計時器的種子初始化隨機數產生器。
要在指定範圍內產生隨機整數,請使用以下公式。
randomValue = CInt(Math.Floor((upperbound - lowerbound + 1) * Rnd())) + lowerbound
此處 是 upperbound 該範圍內的最高數, lowerbound 是該範圍內的最低數值。
備註
若要重複隨機數序列,請先以負參數呼叫 Rnd ,再用數字參數使用 Randomize 。
Important
由於陳述 Random 和函式 Rnd 從種子值開始,產生的數字落在有限範圍內,因此熟悉演算法的人可能能預測結果。 因此, Random 該陳述句與 Rnd 函數不應用於產生用於密碼學的隨機數。
另請參閱
適用於
Rnd(Single)
回傳一個類型 Single為 的隨機數字。
public:
static float Rnd(float Number);
public static float Rnd(float Number);
static member Rnd : single -> single
Public Function Rnd (Number As Single) As Single
參數
傳回
若數小於零,Rnd 每次生成相同數值,並以 Number 種子為基礎。 若數大於零,Rnd 會產生序列中的下一個隨機數。 若數為零,Rnd 生成最近產生的數值。 若未提供數字,Rnd 會產生序列中的下一個隨機數。
範例
此範例使用函 Rnd 數生成一個範圍介於1到6的隨機整數值。
' Initialize the random-number generator.
Randomize()
' Generate random value between 1 and 6.
Dim value As Integer = CInt(Int((6 * Rnd()) + 1))
備註
該 Rnd 函數回傳的值小於1,但大於或等於零。
的 Number 值決定了如何 Rnd 產生隨機數。
對於任一初始種子,產生相同的數字序列,因為每次對 Rnd 函式的呼叫都使用先前產生的數字作為序列下一數的種子。
在呼叫 Rnd之前,使用 Randomize 無參數的語句,以基於系統計時器的種子初始化隨機數產生器。
要在指定範圍內產生隨機整數,請使用以下公式。
randomValue = CInt(Math.Floor((upperbound - lowerbound + 1) * Rnd())) + lowerbound
此處 是 upperbound 該範圍內的最高數, lowerbound 是該範圍內的最低數值。
備註
若要重複隨機數序列,請先以負參數呼叫 Rnd ,再用數字參數使用 Randomize 。 使用 Randomize 相同值 Number 的 則不會重複前一個序列。
Important
由於陳述 Random 和函式 Rnd 從種子值開始,產生的數字落在有限範圍內,因此熟悉演算法的人可能能預測結果。 因此, Random 該陳述句與 Rnd 函數不應用於產生用於密碼學的隨機數。