Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

National Lottery Simulator Help!!

ReiderReider Posts: 36Member
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!

«1

Comments

  • GideonOmegaGideonOmega Posts: 617Member
    I you want to check the numbers to see how many match then you could do the following:
    [code]
    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
    [/code]

    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:
    [code]
    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
    [/code]

    there is probably an easier way to do this.



    [blue]
    C:Dos
    C:Dos Run
    Run Dos Run
    [/blue]

  • ReiderReider Posts: 36Member
    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
  • GideonOmegaGideonOmega Posts: 617Member
    : 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

    [code]
    Dim MyNum(6),WinnningNum(6) as Integer
    dim x,intTotal as Integer

    'load your winning num array with test data
    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
    [/code]

    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?

    [blue]
    C:Dos
    C:Dos Run
    Run Dos Run
    [/blue]

  • ReiderReider Posts: 36Member
    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??
  • GideonOmegaGideonOmega Posts: 617Member
    : 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:
    [code]
    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
    [/code]

    [blue]
    C:Dos
    C:Dos Run
    Run Dos Run
    [/blue]

  • ReiderReider Posts: 36Member
    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?
  • ReiderReider Posts: 36Member
    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??

    Private Sub Form_Load()
    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

  • ReiderReider Posts: 36Member
    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!!
  • GideonOmegaGideonOmega Posts: 617Member
    : 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.
    [code]
    Dim Winningnum(6) as Integer
    ...code here
    ...
    ...
    if newvalue = Winningnum(x) then
    ...more code
    [/code]

    [blue]
    C:Dos
    C:Dos Run
    Run Dos Run
    [/blue]

  • ReiderReider Posts: 36Member
    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!
«1
Sign In or Register to comment.