|
-
Jun 19th, 2001, 07:29 PM
#1
Thread Starter
Addicted Member
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.
-
Jun 19th, 2001, 07:44 PM
#2
Addicted Member
Put a Command Button (Command1) on your form & a list Box (List1)
Add the following code:
VB Code:
Private Sub Command1_Click()
Dim NumbersFoundList As String
Dim NumbersFound As Integer
Dim CurrentNumber As Integer
Randomize
List1.Clear
NumbersFound = 0
NumbersFoundList = ""
Do While NumbersFound < 16
CurrentNumber = Int(Rnd() * 16) + 1
' If the number isn't already found
If InStr(NumbersFoundList, "[" & CurrentNumber & "]") = 0 Then
NumbersFound = NumbersFound + 1
NumbersFoundList = NumbersFoundList & "[" & CurrentNumber & "]"
List1.AddItem CurrentNumber
End If
Loop
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
-
Jun 19th, 2001, 07:57 PM
#3
Thread Starter
Addicted Member
Thanks Nathan, that's exactly I was looking for
Ideas are dime a dozen.
People who put them into action are priceless.
-
Jun 19th, 2001, 07:57 PM
#4
Addicted Member
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:
Dim NumbersFoundList As String
Dim NumbersFound As Integer
Dim CurrentNumber As Integer
Randomize
List1.Clear
NumbersFound = 0
Do While NumbersFound < 16
CurrentNumber = Int(Rnd() * 16) + 1
If InStr(NumbersFoundList, "[" & CurrentNumber & "]") = 0 Then
cmdCaption(NumbersFound).Caption = CurrentNumber
NumbersFound = NumbersFound + 1
NumbersFoundList = NumbersFoundList & "[" & CurrentNumber & "]"
End If
Loop
Hope this helps,
Nathan.
-
Jun 19th, 2001, 07:59 PM
#5
Addicted Member
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...
-
Jun 19th, 2001, 08:01 PM
#6
Thread Starter
Addicted Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|