MS Access Rnd() Function
The Rnd()
function in MS Access generates pseudo-random numbers. These are numbers that appear random but are actually determined by a mathematical algorithm. They're useful for simulations, generating test data, or adding an element of randomness to your applications.
Rnd(): Definition and Usage
It's crucial to understand that Rnd()
, by itself, might produce the same "random" number repeatedly if you don't first use the Randomize()
function to seed the random number generator. Randomize()
initializes the generator with a more unpredictable value, making the results of subsequent Rnd()
calls more varied.
Syntax and Parameter Values
The syntax for Rnd()
depends on whether you want a random number between 0 (inclusive) and 1 (exclusive) or a random number within a specific range:
Syntax
-- For a random number between 0 (inclusive) and 1 (exclusive):
Rnd
-- For a random integer between lowerbound and upperbound (inclusive):
Int((upperbound - lowerbound + 1) * Rnd + lowerbound)
Examples
Generating Random Numbers in Specific Ranges
This example generates one random integer between 1 and 10 and another between 100 and 499.
Syntax
SELECT
Int((10 - 1 + 1) * Rnd + 1) AS RandNumBetween1and10,
Int((499 - 100 + 1) * Rnd + 100) AS RandNumBetween100and499;
Output
RandNumBetween1and10 | RandNumBetween100and499
---------------------------------------------
(A random number 1-10) | (A random number 100-499)