By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,439 Members | 1,311 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,439 IT Pros & Developers. It's quick & easy.

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

AccessQuestion
P: 13
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]

Share this Question
Share on Google+
3 Replies


100+
P: 675
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
Expert 100+
P: 983
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

AccessQuestion
P: 13
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.