How do I generate a random number in Access 2003?

G

Guest

I have a need to randomly select a record from a related table and need to
know how to generate a random number in Access so that I can accomplish this
task. Is there a mathematical function in VBA that can be used, for example?
 
T

Tom Lake

John said:
I have a need to randomly select a record from a related table and need to
know how to generate a random number in Access so that I can accomplish
this
task. Is there a mathematical function in VBA that can be used, for
example?

Rnd will generate a random number in the interval [0,1) (includes 0 but not
1)

To generate a random number between 1 and n for selecting a record,
do this

Int(Rnd * n) + 1

Make n = the number of records in the table to be able to select all
records.

Tom Lake
 
G

Guest

Tom,

I read in my book on Access that there is some command that is called
Randomize that is executed before the Rnd function. Is this necessary and,
if so, what is its purpose? Does it ensure that the result that is obtained
is truly as random as possible (instead of just getting the same result again
and again because the Rnd function has not been initialized) or what?

--John
 
K

kenneth.stark

John said:
Tom,

I read in my book on Access that there is some command that is called
Randomize that is executed before the Rnd function. Is this necessary and,
if so, what is its purpose? Does it ensure that the result that is obtained
is truly as random as possible (instead of just getting the same result again
and again because the Rnd function has not been initialized) or what?

--John

John,

Randomize uses number to initialize the Rnd function's random-number
generator, giving it a new seed value. If you omit number, the value
returned by the system timer is used as the new seed value. If
Randomize is not used, the Rnd function (with no arguments) uses the
same number as a seed the first time it is called, and thereafter uses
the last generated number as a seed value.
Hope this helps.

Ken
 
K

Ken

To finish answering your question, yes... using Randomize does ensure
that the value returned by the formula is as random as possible by
changing the seed value that feeds the random number generator.

Ken
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top