Rnd not very Random

AN60

Registered User.
Local time
Today, 11:10
Joined
Oct 25, 2003
Messages
283
I have a small unbound form which has a txt box with code something like =Rnd((20-1+1)*Rnd()+1)) to generate random numbers. When the db and form are first opened the field always contains the same number. Using F9 to refresh will generate another number but it is always the same too.

If the form is closed then reopened (db is not closed) the field will happily generate a random number each time the form is opened or if F9 is used.

I have tried Recalc in numerous events but each time it failed to work. It's late at night here so I'm hoping the morning will bring a clearer outlook???

Has anyone got any suggestions? :confused:
 
I never use RND but what about adding another RND().

=Rnd((20-1+1)*Rnd()+1))+Rnd()+1

Or multiplying RND X the current serial date/time.
 
Call Randomize() prior to calling Rnd()

This generates the seed value for generating random numbers -
Seed - "An initial value used to generate pseudorandom numbers. For example, the Randomize statement creates a seed number used by the Rnd function to create unique pseudorandom number sequences"
 
Thanks for your assistance. Where should I use randomise() to ensure it works?
 
It needs to run any time prior to when the Rnd() function runs. You can put the code in the form's Open event.
 
Sometimes Access can be fustrating. I have added Randomize to the form Open Event and then almost every other event plus used recalc at a variety of places as well,but still no luck. I've read almost every post on randomize but haven't been able to solve my small problem. Should Randomize be typed with or without the brackets ()? I value my hair but it's getting thinner by the hour.
 
Let me post the help entry here for you since you must have had difficulty in finding it:
Code:
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.
And here's the example:
Code:
Randomize Statement Example
This example uses the Randomize statement to initialize the random-number generator. Because the number argument has been omitted, Randomize uses the return value from the Timer function as the new seed value.

Dim MyValue
Randomize    ' Initialize random-number generator.

MyValue = Int((6 * Rnd) + 1)    ' Generate random value between 1 and 6.

To answer your specific question, the square brackets are a convention used in Help to indicate an optional argument.
 
PH
Thank you for your assistance.
I had found and tried the following in help, might I say with negative results;
Randomize Statement Example
This example uses the Randomize statement to initialize the random-number generator. Because the number argument has been omitted, Randomize uses the return value from the Timer function as the new seed value.

Dim MyValue
Randomize ' Initialize random-number generator.

MyValue = Int((6 * Rnd) + 1) ' Generate random value between 1 and 6.

I assume that by using some code in the [number] I might be on the right track. I'll go try that now. By the way randomize will not pop up in help if typed as such.
 
When you ask for help with VBA, you MUST ask for it from the VBA window. I know this is stupid but it wasn't my decision to break help this way.
 
Yes I just figured out how they have presented things in Help. Occasionally I would stumble on randomize then couldn't find it when I wanted it. Unusual but I guess it fine tunes things a bit. Any suggestions on what I should put in the [number]. ie Does it matter or can it be anything that will generate a number?

Thanks for your help on this subject, it is appreciated.
 
AN60,

You can just just use the Randomize statement by itself.
Code:
Randomize
Another common practice if to use 0 or the Now() function
Code:
Randomize ([COLOR=Blue][B]0[/B][/COLOR])

Randomize ([COLOR=Blue][B]Now()[/B][/COLOR])
I've got some sample code for generating a random whole number between two numbers with some explanation, see here.
 

Users who are viewing this thread

Back
Top Bottom