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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • AccessQuestion
    New Member
    • Jun 2010
    • 13

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

    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:

    Code:
    MyWeight = Int(1000 * Rnd())
    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???
  • OldBirdman
    Contributor
    • Mar 2007
    • 675

    #2
    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:

    Code:
    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.

    Code:
    Randomize [number]

    Comment

    • Oralloy
      Recognized Expert Contributor
      • Jun 2010
      • 988

      #3
      Rnd() is a pseudo random number generator.

      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.

      Comment

      • AccessQuestion
        New Member
        • Jun 2010
        • 13

        #4
        Ok, thanks! I completely get it now.

        Comment

        Working...