## VBA

Moderators: PavlinII
Number of posts: 3000

This Forum Only

National Lottery Simulator Help!! Posted by Reider on 11 Dec 2003 at 8:34 AM
Hi there, i need to produce a national lottery simulator but really don't know where to start.

I know I need to use two arrays for the winning numbers and for my selected numbers and then I need to use loops to check these numbers against each other, this is as far as I have got though, whenever I try to check the numbers against each other it just doesen't seem to recognise matches. The winning numbers also need to be random so I know I can use Int((49 * Rnd) + 1) to do this but some numbers may be duplicated so how can I get around this. The really helpful thing is the work I done was on my computer which was being fixed and the backup copy I kept is now apparantley corrupt so I'm getting pretty desperate!! Please help!

Really stuck here, any help would be greatly appreciated!

Re: National Lottery Simulator Help!! Posted by GideonOmega on 11 Dec 2003 at 12:11 PM
I you want to check the numbers to see how many match then you could do the following:
```dim x,intTotal as Integer
'assuming you have a six number lotto
intTotal = 0
for x = 0 to 5
If YourNum(x) = WinnningNum(x) then
intTotal = intTotal + 1 ' this will tell you how many winning numbers you have
End if
Next x
```

as for your duplicate numbers the only way I can think to do it is to generate the number then check it in a loop against the previously selected numbers then if any matches are found get a different random number, perhaps the best way to control this would be to make a function for the random number and then do something like the following:
```dim x,intcount,newvalue as integer
dim blnPass as Boolean

intcount = 0
do while intcount < 6
newvalue = RandomFunc
blnPass = true
for x = 0 to 5
if newvalue = Winningnum(x) then
blnpass= false
End if
next x
if blnpass = true then
Winningnum(intcount) = newvalue
intcount = intcount + 1
end if
Loop
```

there is probably an easier way to do this.

C:\Dos
C:\Dos Run
Run Dos Run

Re: National Lottery Simulator Help!! Posted by Reider on 12 Dec 2003 at 9:07 AM
Thanks for the help which has been very useful, I'm concentrating first on trying to get the matches and checking right first and then incorporate the random numbers, I have tried your solution but am having troubles with it, I think I am entering the data from the text boxes into the arrays wrong, could you please give me a little more help.

Thanks

Reider
Re: National Lottery Simulator Help!! Posted by GideonOmega on 12 Dec 2003 at 1:16 PM
: Thanks for the help which has been very useful, I'm concentrating first on trying to get the matches and checking right first and then incorporate the random numbers, I have tried your solution but am having troubles with it, I think I am entering the data from the text boxes into the arrays wrong, could you please give me a little more help.
:
: Thanks
:
: Reider
:

Ok, assuming that your text boxs are as follows txtNum1 - txtNum6
The following code will enter the numbers into an array called MyNum

```Dim MyNum(6),WinnningNum(6) as Integer
dim x,intTotal as Integer

WinningNum(0) = 12: WinningNum(1) = 15
WinningNum(2) = 22: WinningNum(3) = 28
WinningNum(4) = 34: WinningNum(5) = 43

MyNum(0) = val(txtNum1.text): MyNum(1) = val(txtNum2.text)
MyNum(2) = val(txtNum3.text): MyNum(3) = val(txtNum4.text)
MyNum(4) = val(txtNum5.text): MyNum(5) = val(txtNum6.text)

'if you then wanted to test to see if the values where correct you could do the following
for x = 0 to 5
debug.print MyNum(x) 'will print the value of x position in the array into the immediate window
next x

Now you have numbers you can test

'assuming you have a six number lotto
intTotal = 0
for x = 0 to 5
If MyNum(x) = WinnningNum(x) then
intTotal = intTotal + 1 ' this will tell you how many winning numbers you have
End if
Next x
```

now to test the above code you just put in the same numbers as the test data into your text box's then remove a winning num and place a wrong one in to test it.

Was that what you where asking for?

C:\Dos
C:\Dos Run
Run Dos Run

Re: National Lottery Simulator Help!! Posted by Reider on 15 Dec 2003 at 8:34 AM
Right I finally seem to be getting somewhere with this now, I've got the whole checking procedure working which seems to be counting the number of matches correctly, now all I need to do is to incorporate the random winning numbers. The way I have decided to do it (which probably isn't the easiest way but oh well!) is to randomly create each of the numbers seperatley using the text boxes and then putting them into the array, so so far I have got the simple part.

In the form load I have:
txtWinningNumbers(0).Text = Int((49 * Rnd) + 1)
txtWinningNumbers(1).Text = Int((49 * Rnd) + 1)
txtWinningNumbers(2).Text = Int((49 * Rnd) + 1)
txtWinningNumbers(3).Text = Int((49 * Rnd) + 1)
txtWinningNumbers(4).Text = Int((49 * Rnd) + 1)
txtWinningNumbers(5).Text = Int((49 * Rnd) + 1)

Now what I need is some kind of looped if statement that basically says

Loop until all of the random numbers are different.

Could I do this simply by saying

Loop until txtWinningNumbers(0), txtWinningNumbers(1) ...... are not the same???? How would I enter this in correct code??
Re: National Lottery Simulator Help!! Posted by GideonOmega on 16 Dec 2003 at 12:27 PM
: Right I finally seem to be getting somewhere with this now, I've got the whole checking procedure working which seems to be counting the number of matches correctly, now all I need to do is to incorporate the random winning numbers. The way I have decided to do it (which probably isn't the easiest way but oh well!) is to randomly create each of the numbers seperatley using the text boxes and then putting them into the array, so so far I have got the simple part.
:
: In the form load I have:
: txtWinningNumbers(0).Text = Int((49 * Rnd) + 1)
: txtWinningNumbers(1).Text = Int((49 * Rnd) + 1)
: txtWinningNumbers(2).Text = Int((49 * Rnd) + 1)
: txtWinningNumbers(3).Text = Int((49 * Rnd) + 1)
: txtWinningNumbers(4).Text = Int((49 * Rnd) + 1)
: txtWinningNumbers(5).Text = Int((49 * Rnd) + 1)
:
: Now what I need is some kind of looped if statement that basically says
:
: Loop until all of the random numbers are different.
:
: Could I do this simply by saying
:
: Loop until txtWinningNumbers(0), txtWinningNumbers(1) ...... are not the same???? How would I enter this in correct code??
:

Try something like this:
```dim x,intcount,newvalue as integer
dim blnPass as Boolean

Randomize 'you need this for random numbers

intcount = 0
do while intcount < 6
newvalue = (49 - 0 + 1) * Rnd + 1
blnPass = true
for x = 0 to 5
if newvalue = Winningnum(x) then
blnpass= false
End if
next x
if blnpass = true then
Winningnum(intcount) = newvalue
intcount = intcount + 1
end if
Loop
```

C:\Dos
C:\Dos Run
Run Dos Run

Re: National Lottery Simulator Help!! Posted by Reider on 19 Dec 2003 at 9:20 AM
I have tried your solution word for word but am obviously doing something wrong because when I try running the program in the line below....

If newvalue = WinningNum(x) then

....it highlights WinningNum and gives me the error message, "Compile error, sub or function not defined."

What am I doing wrong?
Re: National Lottery Simulator Help!! Posted by Reider on 19 Dec 2003 at 10:09 AM
Fingers crossed but I seem to have cracked it, it may be a bit long winded and simple but I seem to have it working, can you see any problems with the code below which I haven't noticed??

Dim count As Integer
count = 0
Randomize
Do Until count = 5
txtWinningNumbers(0).Text = Int((49 * Rnd) + 1)
txtWinningNumbers(1).Text = Int((49 * Rnd) + 1)
txtWinningNumbers(2).Text = Int((49 * Rnd) + 1)
txtWinningNumbers(3).Text = Int((49 * Rnd) + 1)
txtWinningNumbers(4).Text = Int((49 * Rnd) + 1)
txtWinningNumbers(5).Text = Int((49 * Rnd) + 1)

If txtWinningNumbers(0).Text = txtWinningNumbers(1).Text Or txtWinningNumbers(2).Text Or txtWinningNumbers(3).Text Or txtWinningNumbers(4).Text Or txtWinningNumbers(5).Text Then

Else
count = count + 1
End If

If txtWinningNumbers(1).Text = txtWinningNumbers(2).Text Or txtWinningNumbers(3).Text Or txtWinningNumbers(4).Text Or txtWinningNumbers(5) Then

Else
count = count + 1
End If

If txtWinningNumbers(2).Text = txtWinningNumbers(3).Text Or txtWinningNumbers(4).Text Or txtWinningNumbers(5).Text Then

Else
count = count + 1
End If

If txtWinningNumbers(3).Text = txtWinningNumbers(4).Text Or txtWinningNumbers(5).Text Then

Else
count = count + 1
End If

If txtWinningNumbers(4).Text = txtWinningNumbers(5).Text Then

Else
count = count + 1
End If

Loop

End Sub

Re: National Lottery Simulator Help!! Posted by Reider on 19 Dec 2003 at 10:48 AM
Well in reply to my own question I've realised it doesen't work after a bit of testing I can see it still produces duplicate numbers.

I think I just need an idiot proof explanation of how to produce non duplicate random numbers that can go into a textbox array.

Sorry to keep bugging about it all, I think I'm just stupid, Ill try and stop bothering you asap!!
Re: National Lottery Simulator Help!! Posted by GideonOmega on 19 Dec 2003 at 1:27 PM
: Well in reply to my own question I've realised it doesen't work after a bit of testing I can see it still produces duplicate numbers.
:
: I think I just need an idiot proof explanation of how to produce non duplicate random numbers that can go into a textbox array.
:
: Sorry to keep bugging about it all, I think I'm just stupid, Ill try and stop bothering you asap!!
:

I'm sorry, I should have made sure to say that WinningNum(x) in the example must be declared as Either:
(Dim),(Private),(Public) Winningnum(6) as Integer
if you don't do this then the compiler thinks that winningnum(x) is a function or sub and that you are trying to pass it the value of x, then it starts looking for the function and when it dosen't find it, it will generate the error you posted, so try the example I left for you and make sure that it is defined as an array,(sorry I forgot to put it in the code) then let me know if it works.
```Dim Winningnum(6) as Integer
...code here
...
...
if newvalue = Winningnum(x) then
...more code
```

C:\Dos
C:\Dos Run
Run Dos Run

Re: National Lottery Simulator Help!! Posted by Reider on 20 Dec 2003 at 7:46 AM
Well I've tested it around 50 times now and so far I've not got any duplicate random numbers so I've either been very lucky or on the other hand which is that yes it's working!! Finally!! The only thing I had to change was put Int in front of the random number function because it was giving me decimals!

Thanks very much though, you've been a great help!
Glad to help Posted by GideonOmega on 20 Dec 2003 at 11:10 AM
I'm glad I was able to help, thats what we are here for.

C:\Dos
C:\Dos Run
Run Dos Run

## Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic