Results 1 to 6 of 6

Thread: Need to generate random numbers

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Apr 2001
    Posts
    254

    Wink Need to generate random numbers

    Hello all,

    I need to generate random numbers from 1 to 16. I have 16 buttons on the form and I want to give each of those a different number at startup. In other words every button will have a different number ranging between 1 to 16.

    Can someone help me with the code.

    Thanks in advance
    Ideas are dime a dozen.
    People who put them into action are priceless.

  2. #2
    Addicted Member GungaDin's Avatar
    Join Date
    Apr 2001
    Location
    Brisbane, Australia
    Posts
    146
    Put a Command Button (Command1) on your form & a list Box (List1)

    Add the following code:

    VB Code:
    1. Private Sub Command1_Click()
    2.  
    3.     Dim NumbersFoundList As String
    4.     Dim NumbersFound As Integer
    5.     Dim CurrentNumber As Integer
    6.    
    7.     Randomize
    8.    
    9.     List1.Clear
    10.    
    11.     NumbersFound = 0
    12.     NumbersFoundList = ""
    13.     Do While NumbersFound < 16
    14.         CurrentNumber = Int(Rnd() * 16) + 1
    15.         ' If the number isn't already found        
    16.         If InStr(NumbersFoundList, "[" & CurrentNumber & "]") = 0 Then
    17.             NumbersFound = NumbersFound + 1
    18.             NumbersFoundList = NumbersFoundList & "[" & CurrentNumber & "]"
    19.             List1.AddItem CurrentNumber
    20.         End If
    21.     Loop
    22.    
    23. End Sub

    You can change this for your text boxes easy enough.

    I'm sure there are other ways, but this one works.

    Hope this helps,

    Nathan

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Apr 2001
    Posts
    254
    Thanks Nathan, that's exactly I was looking for
    Ideas are dime a dozen.
    People who put them into action are priceless.

  4. #4
    Addicted Member GungaDin's Avatar
    Join Date
    Apr 2001
    Location
    Brisbane, Australia
    Posts
    146
    Well, that code basically gives you what you want.

    Make a Control array of Command buttons called cmdCaption and the code will be as follows:

    VB Code:
    1. Dim NumbersFoundList As String
    2.     Dim NumbersFound As Integer
    3.     Dim CurrentNumber As Integer
    4.    
    5.     Randomize
    6.    
    7.     List1.Clear
    8.    
    9.     NumbersFound = 0
    10.     Do While NumbersFound < 16
    11.         CurrentNumber = Int(Rnd() * 16) + 1
    12.         If InStr(NumbersFoundList, "[" & CurrentNumber & "]") = 0 Then
    13.             cmdCaption(NumbersFound).Caption = CurrentNumber
    14.             NumbersFound = NumbersFound + 1
    15.             NumbersFoundList = NumbersFoundList & "[" & CurrentNumber & "]"
    16.         End If
    17.     Loop

    Hope this helps,

    Nathan.

  5. #5
    Addicted Member GungaDin's Avatar
    Join Date
    Apr 2001
    Location
    Brisbane, Australia
    Posts
    146
    Man am I confused. I'm sure I just read a message saying that wasn't quite what you wanted.

    Hmmm..... Me thinks I best lay off the Metho...

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Apr 2001
    Posts
    254
    Yeah, I just deleted that post because I was replying to the first guy's post. I knew what he was telling me, but as soon as I saw your post I tried and it worked the same way I wanted.

    Thanks again man!!!
    Ideas are dime a dozen.
    People who put them into action are priceless.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width