| | |
Random Number Problem For Lottery Program
Please support our Visual Basic 4 / 5 / 6 advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jul 2006
Posts: 19
Reputation:
Solved Threads: 0
Hi, and thanks for reading, I only hope you have a chance of helping me out, whoever you may be i thank you in advance.
Here is the problem I am up against.
My program is random number selector like a lottery number picker. I made it so it picks all the numbers and each is randomized. But of course as each number is random, it means sometimes the numbers turn out the same.
If i used 6 numbers for example, 1,2,3,4,5,6 like the lotto. Numbers 1 and 6 may generate the same number. I would like to make it so the program knows it has already selected a number and not to select the same number number again in that line. I hope this makes sense. A part of the code that is relavent is below and this is the exe file http://www.andoverhydroponics.co.uk/Lottery.zip
Sorry for posting the link, but the upload file to attatch on the forum did not work.
Any tips would be great Thanks. Bill
Here is the problem I am up against.
My program is random number selector like a lottery number picker. I made it so it picks all the numbers and each is randomized. But of course as each number is random, it means sometimes the numbers turn out the same.
If i used 6 numbers for example, 1,2,3,4,5,6 like the lotto. Numbers 1 and 6 may generate the same number. I would like to make it so the program knows it has already selected a number and not to select the same number number again in that line. I hope this makes sense. A part of the code that is relavent is below and this is the exe file http://www.andoverhydroponics.co.uk/Lottery.zip
Sorry for posting the link, but the upload file to attatch on the forum did not work.
Any tips would be great Thanks. Bill
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
Private Sub cmdLotto_Click() lblLotto1.Caption = Str(Int(Rnd * 49) + 1) lblLotto2.Caption = Str(Int(Rnd * 49) + 1) lblLotto3.Caption = Str(Int(Rnd * 49) + 1) lblLotto4.Caption = Str(Int(Rnd * 49) + 1) lblLotto5.Caption = Str(Int(Rnd * 49) + 1) lblLotto6.Caption = Str(Int(Rnd * 49) + 1) End Sub
Last edited by Comatose; Aug 28th, 2006 at 5:50 am. Reason: Pointless Bold
Just look at the numbers already chosen and if the current value is a duplicate, choose again. This can be done in a loop. You might consider making lblLotto a indexed value instead of separate values.
This way you can set up a for loop to load the numbers and within that a while loop to test for duplicates.
Also, you don't need to bold your posts. We can read them just fine.
This way you can set up a for loop to load the numbers and within that a while loop to test for duplicates.
Also, you don't need to bold your posts. We can read them just fine.
•
•
•
•
Thanks, i have not yet learnt loops. I will learn this, it is in my hardback tutorial somewhere only i just havent got there yet. But i understand what you mean, it will give me somthing to work on thanks again bill.
And who's Bill?So dont try. It's not necessary to bold the title. And if you do you will get people to avoid your posts rather than help because you would look pushy and/or arrogant.
Once you figure out the loops, i would kindly suggest that you initilize your random function before calling it.
I'm sure one of the expert residents (nudge at comatose) can explain a lot more as i'm not 100% sure how this is done in VB, but each time you start your exe, the first set of numbers are always the same, second set the same, etc.
Just some food for thought.
I'm sure one of the expert residents (nudge at comatose) can explain a lot more as i'm not 100% sure how this is done in VB, but each time you start your exe, the first set of numbers are always the same, second set the same, etc.
Just some food for thought.
------------------------------------------------------------
If you see no coffee in my immediate vicinity, speak slowly and use small words....
ConnectNL Directory | Blog
If you see no coffee in my immediate vicinity, speak slowly and use small words....
ConnectNL Directory | Blog
•
•
Join Date: Jul 2006
Posts: 19
Reputation:
Solved Threads: 0
I have now worked out how to prevent the same numbers appearing each time and made the fix. I just added a Randomize line to the code releting to the command button. So randomize is now in both the Command Button and the frame.Solved that problem, I just need to learn how make a loop and how to make the numbers appear in order, just to smarten it up. I am self taught and getting there slowley. Here is the amended code i made for cmd button
The new exe is uploaded. Thanks again Bill
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
Private Sub cmdLotto_Click() Randomize lblLotto1.Caption = Str(Int(Rnd * 49) + 1) lblLotto2.Caption = Str(Int(Rnd * 49) + 1) lblLotto3.Caption = Str(Int(Rnd * 49) + 1) lblLotto4.Caption = Str(Int(Rnd * 49) + 1) lblLotto5.Caption = Str(Int(Rnd * 49) + 1) lblLotto6.Caption = Str(Int(Rnd * 49) + 1) End Sub
The new exe is uploaded. Thanks again Bill
Last edited by Comatose; Aug 28th, 2006 at 5:50 am. Reason: Code Tags Are Added With [CODE] and closed with [/CODE]
•
•
Join Date: Jul 2006
Posts: 19
Reputation:
Solved Threads: 0
If Randomize is in the form only, the numbers display the same on start up, each time. They are only randomised after clicking the command button once.
When i placed randomize in the code for the command button, it randomized the numbers before numbers are selected by random.
As you realize I am new to this but that was my theory, I tried it, and it works.
But as I realy havent much of a clue what im doing, if you told me what i just said is not right, I will take it on board. Thanks
When i placed randomize in the code for the command button, it randomized the numbers before numbers are selected by random.
As you realize I am new to this but that was my theory, I tried it, and it works.
But as I realy havent much of a clue what im doing, if you told me what i just said is not right, I will take it on board. Thanks
•
•
•
•
I'm sure one of the expert residents (nudge at comatose) can explain a lot more....
Here is a small breakdown of the way it works. When you call the rnd function, it keeps the seed value in memory. Calling rnd again will give you the same number. When you call randomize, it resets this stored value. See, computers generate Psuedo-Random numbers. Think of a very long list of numbers, and the computer always returns the values in the same order, but it doesn't always Start at the same place in the list.
Waltp is right, in that you only actually need to reset the seed once. Form_load is a nice place for that, but it won't cause any problems re-seeding the psuedo-number generator. It is, however, not very good programming style to re-seed the number generator.
I call randomize with timer as the seed (
randomize timer), which just make it a little more psuedo-random. Should you want to know the last random number called, pass 0 to rnd, and it will tell you the last random number generated, such as lastrand = rnd(0). I have no clue why you would want to do that (unless it's to make sure the new number is not the same), but you can.The rnd function returns values smaller than 1, but greater than or equal to 0, which sort of sucks. You can fix this, though with a little function:
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
Public Function Rand(LowNum as long, HighNum as long) as long xRand = int((HighNum - LowNum + 1) * Rand) + Low end function
![]() |
Similar Threads
- random number display the same (C#)
- Random number generator (Java)
- Lottery Program (Java)
Other Threads in the Visual Basic 4 / 5 / 6 Forum
- Previous Thread: How to activate A Window
- Next Thread: Component "comdlg32.ocx" missing
| Thread Tools | Search this Thread |
* 6 429 2007 access activex add age application basic beginner birth bmp calculator cd cells.find click client code college column component connection connectionproblemusingvb6usingoledb copy creat ctrl+f data database datareport date delete dissertations dissertationthesis dissertationtopic edit error excel excelmacro file filename form hardware header iamthwee image inboxinvb internetfiledownload keypress label listbox listview liveperson login looping machine microsoft movingranges number objectinsert open oracle password prime program prompt range-objects readfile reading record refresh remotesqlserverdatabase report retrieve save search sendbyte sites sort sql sql2008 sqlserver subroutine tags textbox time urldownloadtofile vb vb6 vb6.0 vba visual visualbasic visualbasic6 web window windows






