Rnd - VBScript


Overview

The Rnd command generates a random number. It is most useful for generating random values within a specified range.

Syntax

Rnd([number])
  • number: Optional. Value returned by the function (in the range 0 to 1, inclusive).

Options/Flags

None.

Examples


' Print a random number between 0 and 1 (default)
WScript.StdOut.WriteLine Rnd

' Print a random number between 20 and 30
WScript.StdOut.WriteLine (20 + (30 - 20) * Rnd)

' Generate 10 random numbers
For i = 0 to 9 
    WScript.StdOut.Write Rnd & " "
Next

Common Issues

  • Ensure you use the parentheses when calling Rnd without specifying a number.

Integration

Rnd can be combined with other VBScript commands for advanced tasks:

  • Use it with Int to generate a random integer.
  • Pair it with Timer to generate a more secure random number.
  • Utilize it with loops to create random sequences or generate arrays of random numbers.
  • Math.Randomize: Initializes the random number generator.
  • Math.Round: Rounds a number to a specified precision.
  • Math.Floor: Rounds a number down to the nearest integer.
  • Math.Ceiling: Rounds a number up to the nearest integer.