How does Rnd() work? How to store a value.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • zaankanter
    New Member
    • Feb 2008
    • 30

    How does Rnd() work? How to store a value.

    Hi, This is my problem.
    A form (startform) is opened, wich is based on a table, called programvariable s. This is supposed to be a one-record table, wich will hold some values needed to store user-preferences. The form shows some of these preferences.
    Now I want on the event of opening this form, to make a random number (integer), and then to store this number in a field in de table(programva riables), wich field I call res3.
    Then the content of the field can/should be shown in the form I just opened.

    Problem1: trying Rnd() just gets me the same nummer over and over again. This is of course not wat random is supposed to be. Just wat need I to do to make Acces give me a realy random number?
    Problem2: how do I store (without intervention of the user) a number in a table (that is: in the one pre-existing record, not appending a record to the table).

    Have patience with me I realy know very little of VB

    Already many thanks
    Chris
  • Zwoker
    New Member
    • Jul 2007
    • 66

    #2
    Originally posted by zaankanter
    Problem1: trying Rnd() just gets me the same nummer over and over again. This is of course not wat random is supposed to be. Just wat need I to do to make Acces give me a realy random number?
    Before calling Rnd for the first time add a Randomize statement to your code. This uses the system time to create a semi-random starting point for your RND function.

    Originally posted by zaankanter
    Problem2: how do I store (without intervention of the user) a number in a table (that is: in the one pre-existing record, not appending a record to the table).
    You can update the existing record using VBA code - I am assuming you have coded this and are not using MS Access to access your data (If you are you probably need a different solution). An simple example of opening, reading and updating a record in a table is shown below. This is using MS Access 2003. I haven't bother showing any of the DIMs etc...

    Code:
    Randomize
    MyTablet.Open "programvariables", CurrentProject.Connection, adOpenStatic, adLockOptimistic
    MyTable.MoveFirst
    MyTable![res3]=Rnd()
    MyTable.Update
    MyTable.Close
    Does this help?

    By the way, in case you don't realise, the Rnd function returns a value between 0 and 1. If you want an interger value, say a randon number between 1 and 10, then you would need to do something like :
    Code:
    MyTable![res3 ]= Int((Rnd() * 10) +1)

    Comment

    • zaankanter
      New Member
      • Feb 2008
      • 30

      #3
      Originally posted by Zwoker
      Before calling Rnd for the first time add a Randomize statement to your code. This uses the system time to create a semi-random starting point for your RND function.
      <.......>
      By the way, in case you don't realise, the Rnd function returns a value between 0 and 1. If you want an interger value, say a randon number between 1 and 10, then you would need to do something like :
      Code:
      MyTable![res3 ]= Int((Rnd() * 10) +1)
      I'll try this tomorrow in my own project. Will let you know if it works.
      I'm just wondering, why the @@ are the clear and simple statements quoted above (thanks !) not to be found in any helpfunctions of Acces?

      Chris

      Comment

      • zaankanter
        New Member
        • Feb 2008
        • 30

        #4
        Originally posted by Zwoker
        Before calling Rnd for the first time add a Randomize statement to your code. This uses the system time to create a semi-random starting point for your RND function.
        <.......>
        By the way, in case you don't realise, the Rnd function returns a value between 0 and 1. If you want an interger value, say a randon number between 1 and 10, then you would need to do something like :
        Code:
        MyTable![res3 ]= Int((Rnd() * 10) +1)
        I'll try this tomorrow in my own project. Will let you know if it works.
        I'm just wondering, why the @@ are the clear and simple statements quoted above (thanks !) not to be found in any helpfunctions of Acces?

        Chris

        Comment

        • Zwoker
          New Member
          • Jul 2007
          • 66

          #5
          Originally posted by zaankanter
          I'm just wondering, why the @@ are the clear and simple statements quoted above (thanks !) not to be found in any helpfunctions of Acces?
          I'm not sure what you mean by the @@ in your question?
          Which version of MS Access are you using? Everything I mentioned that is a MS Access function is in my help, and I'm using 2003.

          Also, a minor point, but there is an extra "t" in the name I declare in the open statement in my example. If you were to copy and paste it as is, it would fail on the MoveFirst. Just remove the extra letter.

          Regards,
          Zwoker.

          Comment

          • zaankanter
            New Member
            • Feb 2008
            • 30

            #6
            You don't want to know what @@ means! And yes, its probably my own fault, that I don't know my way around in the helpfunction of Acces. I use 2003.

            Although I think your program lines must be the right answer, it doesn't quite work with me yet. These are the lines I programmed (my table has a slightly different spelling, i.e. dutch)
            Private Sub Form_Load()

            If IsNull(DLookup( "[res3]", "programmavaria belen")) Then
            MsgBox "res3 is empty"
            Randomize
            MyTable.Open "programmavaria belen", CurrentProject. Connection, adOpenStatic, adLockOptimisti c
            MyTable.MoveFir st
            MyTable![res3] = Rnd()
            MyTable.Update
            MyTable.Close

            MsgBox (Me![res3])
            End If
            End Sub
            It stops at My.table.open etc.
            There I get "object required", but wich object would that be?

            Comment

            • Zwoker
              New Member
              • Jul 2007
              • 66

              #7
              Originally posted by zaankanter
              You don't want to know what @@ means! And yes, its probably my own fault, that I don't know my way around in the helpfunction of Acces. I use 2003.

              Although I think your program lines must be the right answer, it doesn't quite work with me yet. These are the lines I programmed (my table has a slightly different spelling, i.e. dutch)
              Private Sub Form_Load()

              If IsNull(DLookup( "[res3]", "programmavaria belen")) Then
              MsgBox "res3 is empty"
              Randomize
              MyTable.Open "programmavaria belen", CurrentProject. Connection, adOpenStatic, adLockOptimisti c
              MyTable.MoveFir st
              MyTable![res3] = Rnd()
              MyTable.Update
              MyTable.Close

              MsgBox (Me![res3])
              End If
              End Sub
              It stops at My.table.open etc.
              There I get "object required", but wich object would that be?
              I can only take a guess that you don't have an object set selected in your project, that you need.
              When you are in the code window, click on the Tools menu item and choose References. What items do you have ticked there? It could be something missing there... *shrug*

              Hopefully someone else reading this can point you (and me) in the right direction.

              Comment

              • zaankanter
                New Member
                • Feb 2008
                • 30

                #8
                Thanks for your help so far.
                I'll try to get some others interested too by posting the problem again in its present state

                Comment

                • Scott Price
                  Recognized Expert Top Contributor
                  • Jul 2007
                  • 1384

                  #9
                  Bumping your thread by adding another post to the end of it is usually sufficient if you aren't getting any results. Please refrain in the future from double posting.

                  Here is the Access help file for the Rnd() function. To get this help file yourself, simply position the cursor within the word you want help on and press F1.

                  Rnd Function


                  Returns a Single containing a random number.

                  Syntax

                  Rnd[(number)]

                  The optional number argument is a Single or any valid numeric expression.

                  Return Values

                  If number is Rnd generates
                  Less than zero The same number every time, using number as the seed.
                  Greater than zero The next random number in the sequence.
                  Equal to zero The most recently generated number.
                  Not supplied The next random number in the sequence.



                  Remarks

                  The Rnd function returns a value less than 1 but greater than or equal to zero.

                  The value of number determines how Rnd generates a random number:

                  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:

                  Int((upperbound - lowerbound + 1) * Rnd + lowerbound)

                  Here, upperbound is the highest number in the range, and lowerbound is the lowest number in the range.

                  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.
                  Regards,
                  Scott

                  Comment

                  Working...