Access: Rnd Function

In Access, the Rnd function returns a single containing a random number. You can specify the random number to be a value between 2 user-specified numbers.

The syntax for the Rnd function is:

Rnd ( number )

The argument number can be any valid numeric expression. The Rnd function returns a value less than 1 but greater than or equal to zero. To produce random integers in a given range, use this formula:

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

Here, upperbound is the highest number in the range, and lowerbound is the lowest number in the range.

Example

Int((7-1 +1) * Rnd + 1)              returns a random value between 1 and 6.
Int((30-20 +1) * Rnd + 20)           returns a random value between 20 and 30.

VBA Code

Dim MyNumber 
MyNumber = Int((6-1+1)*Rnd +1) 

This example uses the Rnd function to generate a random integer from 1 to 6. Now the MyNumber variable would contain this value.

SQL query

You can also use the Rnd function in a query.

SELECT Int((10-1+1)*Rnd()+1) AS Expr1 
FROM Orders
admin

admin

Leave a Reply

Your email address will not be published.