![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: May 2005
Location: Charleston, SC
Posts: 11
Rep Power: 0
![]() |
only displays 1 number not 6
the following code is only displaying one number and I am trying to get it display six numbers
Dim number(5) As Integer
Dim subscript As Integer
Dim searchSubscript As Integer
Dim isFound As Boolean
Dim raNumber As Integer
Dim randomGenerator As New Random
number(0) = 5
subscript = number(1)
Do While subscript < number.Length
raNumber = randomGenerator.Next(1, 54)
searchSubscript = 0
Do While searchSubscript < subscript
isFound = False
If searchSubscript = raNumber Then
isFound = True
Else
searchSubscript = searchSubscript + 1
End If
Loop
If isFound = False Then
subscript = raNumber
subscript = subscript + 1
End If
Loop
'display the numbers
Me.xLotteryLabel.Text = (subscript.ToString())
End Sub
End Class |
|
|
|
|
|
#2 |
|
Professional Programmer
Join Date: Mar 2005
Location: Glasgow, Scotland
Posts: 314
Rep Power: 4
![]() |
I can see what you're trying to do there, but you're getting confused between numbers, subscripts, etc. You start off by generating numbers into your array, then you display the numbers (right?). So how about:
Dim I As Integer, J As Integer
Dim intNumber(5) As Integer
Dim intNew As Integer
Dim rndGen As New Random
For I = 0 To 5
intNew = rndGen.Next(1, 54)
For J = 0 To I - 1
If intNumber(J) = intNew Then
I = I - 1
Exit For
End If
Next J
Next I
For I = 0 To 5
lblResult.Caption = lblResult.Caption & intNumber(I)
NextNotice I don't really need the boolean to note whether I've found the number already there; instead, after the new number is generated I search through the array to see if it's already there and take one off I if it is, which will push the outer loop back so it runs this step again next time. Then I Exit For to duck out of the inner For loop to let that happen. This is probably not the best way and isn't exactly what you were asking for but I was finding your code confusing, so I hope you can figure out how to put the two together and that this helps. I also hope it runs - I don't actually have VB handy to try it out!
__________________
"I'm not a genius. Why do I have to suffer?" |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Jun 2006
Location: Michigan
Posts: 13
Rep Power: 0
![]() |
You both appear to be forgetting to populate your array, it might look something like this:
Private Sub FindWinningLottoNumberButton_Click() Dim I As Integer Dim lottoNum As Integer Dim lottoNumArray(5) As Integer For I = 0 To 5 lottoNum = Rand(1, 56) lottoNumArray(I) = lottoNum ' place code to check for unique numbers here Next I For I = 0 To 5 TextBox1.Text = TextBox1.Text & lottoNumArray(I) & " " Next I TextBox1.Text = TextBox1.Text & vbNewLine End Sub Hope this helps! |
|
|
|
|
|
#4 | |
|
Not a user?
Join Date: Sep 2007
Posts: 245
Rep Power: 1
![]() |
Quote:
number(0) is initialized to value 5, number(1) will be initialized to 0, unless that's what you were counting on. |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| c-unix-childprocesses-random number | programmingnoob | C | 7 | Feb 6th, 2007 8:39 PM |
| Unix commands compatible with Windows? | titaniumdecoy | Bash / Shell Scripting | 7 | Oct 5th, 2006 7:25 AM |
| Jython | Jessehk | Coder's Corner Lounge | 2 | Feb 5th, 2006 4:35 AM |
| [tutorial] Python for programming beginners | coldDeath | Python | 30 | Dec 14th, 2005 11:35 AM |
| FiveDigit + RandomeNumber Game. | TecBrain | Java | 0 | Nov 18th, 2005 2:53 PM |