Rnd not very Random

AN60

Registered User.
Local time
Today, 03:28
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?
 
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.
 
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.
 
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