VBScript Rnd Function

## VBScript Tutorial

VB HOME VB How to VB Variables VB Procedures VB Conditional VB Looping VB Summary

## VB Examples

VB Examples

## VB References

VB Functions VB Keywords

# VBScript Rnd Function

Complete VBScript Reference

The Rnd function returns a random number. The number is always less than 1 but greater or equal to 0.

### Syntax

Rnd[(number)]

Parameter | Description
--- | ---

number

Optional. A valid numeric expression

If number is:

- <0 - Rnd returns the same number every time
- >0 - Rnd returns the next random number in the sequence
- =0 - Rnd returns the most recently generated number
- Not supplied - Rnd returns the next random number in the sequence

## Examples

## Example 1

A random number:

<script type="text/vbscript">
document.write(Rnd)
</script>

Note that you will get the same number every time. To avoid this, use the Randomize statement like in Example 2

The output of the code above will be:

0.7055475
Try it yourself »

## Example 2

To avoid getting the same number every time, like in Example 1, use the Randomize statement:

<script type="text/vbscript">
Randomize
document.write(Rnd)
</script>

The output of the code above will be:

0.4758112
Try it yourself »

## Example 3

Here is how to produce random integers in a given range:

<script type="text/vbscript">
Dim max,min
max=100
min=1
Randomize
document.write(Int((max-min+1)*Rnd+min))
</script>

The output of the code above will be:

71
Try it yourself »

Complete VBScript Reference

## Your suggestion:

Your E-mail (optional):

Page address:

Description:

Close [X]

## Thank You For Helping Us!

Your message has been sent to W3Schools.

Close [X]
Search w3schools.com:

## WEB HOSTING

Best Web Hosting UK Reseller Hosting Wow! $5 SSD Hosting

## WEB BUILDING

Download XML Editor
FREE Website BUILDER FREE Website Creator

## WEB RESOURCES

Web Statistics Web Validation

## SHARE THIS PAGE