Results 1 to 9 of 9
  1. #1
    oleBucky's Avatar
    oleBucky is offline Competent Performer
    Windows XP Access 2003
    Join Date
    Feb 2011
    Location
    Inside the Loop
    Posts
    100

    No-So-Random Number

    I am using Rnd() to generate a random number between 1 and 50 for a gaming database project I am working on. The code is activated by a Click event on a command button then the resulting value is passed to a text box for display. I added a "roll" simulator to loop through the numbers to try to give some "body" to a simple click. The problem is that every time the button is clicked after first opening the database the result is 36. It makes me loose faith in the pseudo-random nature of Rnd() if the recurring 36 is not the result of my neophyte coding. Might someone review this code and help me understand this phenomenon?

    Let it be known, the Click event controls several other features of the form. However, for the sake of brevity, I am including the following code that applies to the function in question:


    Code:
    Private Sub cmdRandomGame_Click()
        Dim GameNum As String
        Dim RollSim As String
        Dim WAIT As Double
        GameNum = (1 + Int(50 * Rnd()))
        'Use this to test
        'GameNum = 13
        'Trying to make random number change "visible" to user
        RollSim = GameNum + 1
        Do Until RollSim = GameNum
            If RollSim > 50 Then
                If GameNum = 1 Then
                    RollSim = 1
                Else
                RollSim = 1
                Me.txtRandomCard = RollSim
                WAIT = Timer
                    While Timer < WAIT + 0.01
                    DoEvents  'do nothing
                    Wend
                RollSim = RollSim + 1
                End If
            Else
                Me.txtRandomCard = RollSim
                WAIT = Timer
                    While Timer < WAIT + 0.01
                    DoEvents  'do nothing
                    Wend
                RollSim = RollSim + 1
            End If
        Loop
        
        Me.txtRandomCard = GameNum
        
    End Sub
    As always, thanks in advance to the forum for your input.

  2. #2
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    You have to use Randomize() to set the random seed first.

  3. #3
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Read up on Rnd() in VBA Help. Rnd() works hand-in-hand with the Randomize statement, which you haven't used. I wrote a little payroll lottery program for someone, and tested it extensively. Used correctly, Rnd() will truly return a random number.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Geez, talk about your slow typist!
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    We have to type fast to keep warm here.

  6. #6
    oleBucky's Avatar
    oleBucky is offline Competent Performer
    Windows XP Access 2010 32bit
    Join Date
    Feb 2011
    Location
    Inside the Loop
    Posts
    100
    A quick clarification... I changed my CP after initializing this thread. I am now using Access 2010 Version 14 32-bit.

    In an overall sense, that worked perfectly. However, when entering "Randomize()" in VBA the software gave me a compile error ("Expected: ="). Using "Randomize" only worked. Is this something that will bite me later?

    Another quick addition. I would assume it is necessary to "Randomize" each command button generating random number (I have several). Is that assumption correct?

  7. #7
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    I've not used it before so Paul would be the authority in this thread; even if he is a bit slow in typing.

  8. #8
    boblarson is offline --------
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2011
    Posts
    1,272
    Quote Originally Posted by oleBucky View Post
    A quick clarification... I changed my CP after initializing this thread. I am now using Access 2010 Version 14 32-bit.

    In an overall sense, that worked perfectly. However, when entering "Randomize()" in VBA the software gave me a compile error ("Expected: ="). Using "Randomize" only worked. Is this something that will bite me later?

    Another quick addition. I would assume it is necessary to "Randomize" each command button generating random number (I have several). Is that assumption correct?
    With functions - unless you have something on the left that you are setting equal to (=) you don't use the parens on the function. So by itself you use simply

    Randomize

  9. #9
    oleBucky's Avatar
    oleBucky is offline Competent Performer
    Windows XP Access 2010 32bit
    Join Date
    Feb 2011
    Location
    Inside the Loop
    Posts
    100
    Thanks to all! Another step forward in learning for me.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Random 0 now
    By Mounds in forum Access
    Replies: 4
    Last Post: 04-29-2011, 10:04 AM
  2. Query Random Number Column Manipulation
    By gmontano in forum Queries
    Replies: 4
    Last Post: 10-05-2010, 01:36 PM
  3. Sorta Random Serial Number Generator
    By Cuselco in forum Programming
    Replies: 3
    Last Post: 08-27-2010, 12:05 PM
  4. Random number generators with no repeats
    By CJ508 in forum Access
    Replies: 5
    Last Post: 10-29-2009, 07:30 AM
  5. random record
    By wasim_sono in forum Programming
    Replies: 1
    Last Post: 02-06-2008, 02:42 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums