468,306 Members | 1,297 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,306 developers. It's quick & easy.

How to access random number question (Not random at all)

AccessQuestion
So just a quick question; more out of curiosity than anything. I have a form in which I pop a random number into a text box. Eventually that number will be placed by a user using a scale, but I wanted to simulate that now. Anyway, I used this code:

Expand|Select|Wrap|Line Numbers
  1. MyWeight = Int(1000 * Rnd())
  2.  
I assumed that will give me a random number, however EVERY time I launch my form the random numbers are the same. First 'random number' is always 705, followed by 533, etc. What gives with that??? Has anyone run into that before???
Jun 24 '10 #1

✓ answered by OldBirdman

From Access Help for RND:
For any given initial seed, the same number sequence is generated because each successive call to the Rnd function uses the previous number as a seed for the next number in the sequence.

Before calling Rnd, use the Randomize statement without an argument to initialize the random-number generator with a seed based on the system timer.

To produce random integers in a given range, use this formula:

Expand|Select|Wrap|Line Numbers
  1. Int((upperbound - lowerbound + 1) * Rnd + lowerbound)
Also for RANDOMIZE
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.

Expand|Select|Wrap|Line Numbers
  1. Randomize [number]

3 3924
OldBirdman
675 512MB
From Access Help for RND:
For any given initial seed, the same number sequence is generated because each successive call to the Rnd function uses the previous number as a seed for the next number in the sequence.

Before calling Rnd, use the Randomize statement without an argument to initialize the random-number generator with a seed based on the system timer.

To produce random integers in a given range, use this formula:

Expand|Select|Wrap|Line Numbers
  1. Int((upperbound - lowerbound + 1) * Rnd + lowerbound)
Also for RANDOMIZE
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.

Expand|Select|Wrap|Line Numbers
  1. Randomize [number]
Jun 24 '10 #2
Oralloy
983 Expert 512MB
Looking at the Access 2007 documentation on Rnd(), you will find that it is a pseudo random number generator, and there are four different calling conventions.

In addition there is a specific note about seeding the random number generator:
Before calling Rnd, use the Randomize statement without an argument to initialize the random-number generator with a seed based on the system timer.
Jun 24 '10 #3
Ok, thanks! I completely get it now.
Jul 2 '10 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

By using this site, you agree to our Privacy Policy and Terms of Use.