Rand Duplication

LEXCERM

Registered User.
Local time
Today, 14:56
Joined
Apr 12, 2004
Messages
169
Hi all (I've also posted this on another forum as it's doing my brain in!) :eek:

Code:
Public Function CreateRandomCollNumber()
Dim UpperBound As Long, LowerBound As Long, CheckDupe As Integer
UpperBound = 9999999
LowerBound = 1000000
CreateRandomCollNumber = Int((UpperBound - LowerBound + 1) * Rnd + LowerBound)

Problem:
Running the above function will produce the following numbers:-
7349927, 5800816, 6215667, 3606062.

If the table (which stores the data) is cleared and the database is compacted, the function repeats the numbers in the same sequence:-
7349927, 5800816, 6215667, 3606062.

I've also created a new database and just running the function (no tables). The same numbers appear. When I compact the DB they repeat again.

Questions:
1) Are these numbers stored in my profile only?
2) Are these numbers preset within Access?
3) As a test, can someone else run the above and see what numbers they get?
4) Am I the only person in the world who is un-aware of this feature? :o

Rgds and thanks in advance,
Paul.
 
I also got the same result when I ran your function once.

Then I changed your code as follows:
UpperBound = 9999999
LowerBound = 1000000
Randomize
CreateRandomCollNumber = Int((UpperBound - LowerBound + 1) * Rnd + LowerBound)

I then got different results. read the Help on the RND function.
 
Here is the Help article on Randomize
Randomize Statement


Initializes the random-number generator.
Syntax
Randomize [number]
The optional number argument is a Variant or any valid numeric expression.
Remarks
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.
Note To repeat sequences of random numbers, call Rnd with a negative argument immediately before using Randomize with a numeric argument. Using Randomize with the same value for number does not repeat the previous sequence.
 

Users who are viewing this thread

Back
Top Bottom