## RND Function

The RND Function generates a number that is between 0 and 1. The syntax of the RND Function is:

Rnd([Number]) where:

- Number (
*Optional)*– This is optional and if <0, the function returns the same random number on each call using [Number] as the seed, if =0, the function returns the most recent random number, if >0 the function returns the next generated random number. If blank the default >0, is used.

1 2 3 |
Sub RndNum() MsgBox Rnd() End Sub |

## Generating a Random Number in VBA

In order to generate a random number between two values, you have to use the RND Function in combination with the INT Function (Integer Function) using the following general formula:

- Int(lowerbound + Rnd * ( upperbound – lowerbound + 1 ) )

So, in order to generate a random number between 2 and 30, you would use the following code:

1 2 3 4 5 6 7 8 |
Sub GeneratingARandomNumber() Dim randomNumber As Integer randomNumber = Int(2 + Rnd * (30 - 2 + 1)) Debug.Print randomNumber End Sub |