• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

What is wrong with this code

ninjalearner

New Member
Dear All, I have written the codes below, it returns text as result and keep running endlessly. What i am trying to do is to generate a lottery winning ticket and number. The comments ahead of each of my codes explains what i want to achieve there. I will appreciate if any expert in the house can help me look into my code and make adjustments on it and put comments as to why the adjustment was made. this will help me undertsand my errors. I am a vba learner.

Thanks All.


Dim WinningNumb As String

Dim X As Integer

Dim J As Integer

Dim Tickets As String

Dim TicketNumb As String


' Generate the winning number as a sequence of digits from 0 to 9.


For X = 1 To 100

WinningNumb = ""

For J = 0 To 9

WinningNumb = WinningNumb & Int(Rnd * 5)


Next


' Find how many cards user wants to buy and redim the array to the proper size.


TicketNumb = 0


' Generate the tickets and store them in the array


Do

TicketNumb = TicketNumb + 1

Tickets = ""

' Look for a winner by comparing the winning number to the contents of the array


For J = 0 To 9

Tickets = Tickets & Int(Rnd * 5)


Next


Loop Until Tickets = WinningNumb


' Report the result in a message box


MsgBox "The Winning Ticket is " & "Tickets" & vbCrLf & " The Winning Lottery Number is " & "WinningNumb"


Next


End Sub
 
Hi, ninjalearner!

Unquote "Tickets" and "WinningNumb" strings from MsgBox statement. BTW, both of them will display the same number. What sense?

Regards!
 
Good day ninjalearner

A non VBA answer could be to enter the following into a cell "=RANDBETWEEN(0,9)", every time you press F9 you will get a new number
 
@b(ut)ob(ut)hc


Hi, old dog new tricked!


Good and healthy day.


As far as I could see he's trying to generate random numbers of 10 digits ranging each from 0 to 4. So RANDBETWEEN(0,9) wouldn't do it. After reading your post I firstly thought about REPEAT(RANDBETWEEN(0,4),10), but it actually repeats the same number, i.e., RANDBETWEEN is only called once.


Using formulas the approach might be RANDBETWEEN(0,4)&RANDBETWEEN(0,4)&RANDBETWEEN(0,4)&RANDBETWEEN(0,4)&RANDBETWEEN(0,4)&RANDBETWEEN(0,4)&RANDBETWEEN(0,4)&RANDBETWEEN(0,4)&RANDBETWEEN(0,4)&RANDBETWEEN(0,4).


Regards!
 
Good day SirJB7, a pleasure to hear from you,I just read it as

"Dim Tickets As String

Dim TicketNumb As String


' Generate the winning number as a sequence of digits from 0 to 9."


and did a rand for nine numbers..........but...
 
@b(ut)ob(ut)hc


Hi!


I read too "from 0 to 9", but (there's always a but...) that's give me a 10. Unless your Royal or Imperial System converts it to that annoying distance unit that you call yards :)

The "Int(Rnd * 5)" part generates digits only from 0 to 4 (integer part of numbers from 0,00000000 to 4,99999999, if 8 decimals used).


Regards!


PS: BTW, I almost forget! I quoted a couple of hours ago...

you http://chandoo.org/forums/topic/security-properties-of-a-cell#post-30476
 
SirJB7

As you know I spent many years in engineering and metrics rule.....but I do not think it would be cricket with the good old yards (Imperial). With metrics the whole world speaks with one, except the USA who are very reluctant to join in with every one else
 
Don't forget Liberia & Myanmar

that puts the US in esteemed company.
 
Hi ,


Why don't you go through this code , and improve it wherever you want ?

[pre]
Code:
Public Sub temp()
Dim WinningNumb As Double, X As Double

Dim I As Integer, J As Integer, winning_ticket As Integer
Dim user_input As Integer

Dim Tickets() As Variant

Dim match_found As Boolean

' Generate the winning number as a sequence of digits from 0 to 9.
WinningNumb = Application.WorksheetFunction.RandBetween(1023456789, 9876543210#)

' Find how many cards user wants to buy and redim the array to the proper size.
Do
user_input = Val(InputBox("Enter the number of tickets you want to buy ( 1 to 1000 ) :"))
choice = vbNull
If user_input < 1 Or user_input > 1000 Then
choice = MsgBox("Invalid Input , Limits are 1 and 1000 ", vbRetryCancel)
If choice <> vbRetry Then Exit Sub
End If
Loop Until choice = vbNull
ReDim Tickets(1 To user_input)

' Generate the tickets and store them in the array
For J = 1 To user_input
Do
X = Application.WorksheetFunction.RandBetween(1023456789, 9876543210#)
match_found = False
For I = 1 To user_input
If X = Tickets(I) Then
match_found = True
Exit For
End If
Next
Loop Until Not match_found
Tickets(J) = X
Next

' Look for a winner by comparing the winning number to the contents of the array

match_found = False
For I = 1 To user_input
If Tickets(I) = WinningNumb Then
match_found = True
winning_ticket = I
Exit For
End If
Next

' Report the result in a message box
If match_found Then
MsgBox "The Winning Ticket is " & winning_ticket & vbCrLf & " The Winning Lottery Number is " & WinningNumb
Else
MsgBox "Sorry , you have not won any prize"
End If

End Sub
[/pre]
In case anything leaves you confused , please post back here.


Narayan
 
Back
Top