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

``````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``````

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 …

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.

Good. Something to look forward to... ;) And …

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 …

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.

You only need the randomize command once, so put it in form.load only.

I'm sure one of the expert residents (nudge at comatose) can explain a lot more....

No Pressure There! :eek:

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 …

## All 21 Replies

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.

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.
ps i meant to bold the just thread name only.

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.

Good. Something to look forward to... ;) And who's Bill?

ps i meant to bold the just thread name only.

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.

You are absolutly correct I did not notice thanks.I am going to redesign the program, I have managed to get the numbers correct but only displaying in a message box. Still working on this, I dont mind it's good practice and I appreciate constructive comments. Thanks Bill

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

``````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

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.

You only need the randomize command once, so put it in form.load only.

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

I'm sure one of the expert residents (nudge at comatose) can explain a lot more....

No Pressure There! :eek:

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:

``````Public Function Rand(LowNum as long, HighNum as long) as long
xRand = int((HighNum - LowNum + 1) * Rand) + Low
end function``````

Looping is a fun trick where you can do something a number of times or until a specific condition is met. There are a few kinds of loops, but two of the main ones are a "for loop" and a "do loop". A For loop is a way to execute the same code, a specific number of times. So, If I wanted to add the numbers 1 to 10 in a listbox, I could do this:

``````for I = 1 to 10
next I``````

What happens here, is that the variable I starts at the number 1... so the first time list1.additem is executed, I will be 1. Then, when it hits the line that says "next I", it says "ok, is I equal to 10 yet?", if the answer is no, then it adds 1 to I, and does it all again. So the second time, I will be 2, and then 3, and so on until it reaches 10. This is really helpful when you want to get a certain number of random numbers. If you only need 6, you could do `for i = 1 to 6` . For reasons that I don't want to go into here, MOST VB programmers usually start a for loop at 0. So, to add six items `for i = 0 to 5` . This isn't required, however, but if you view other people's code, you might see this quite often.

The other kind of loop I want to talk about here, is a do loop. This allows a loop to go on and on until a condition is met (just like an if). So an example would be something like:

``````x =1
do until x = 10
x = x + 1
loop``````

This starts the variable x at 1, and loops until x is equal to 10. Inside the loop, it adds 1 to x. So, the loop will run (what, actually 9 times?). Anyway, the point is, it will continue to loop until x becomes 10. This can all be variable too.... this is how some games implement a "quit" feature. It has a command button, that sets a public variable to true. The main loop of the game is a do loop, that is like `do until cancelflag = true` . When the command button is pressed, it changes cancelflag to true, thus ending the loop.

Here, I have actually used these two loops (nesting is when you put one thing inside of another.... so, if I have a for loop, and then inside of the loop, a do loop, I have "nested" the do loop in the for loop) to make a small program that adds a list of 10 numbers to a listbox, that are all random, between 1 and 100, and are never the same in order (never two sixes, or two nines). I'm sure with your lotto program, you'll have to make some modifications, but this is the way I would go about doing it.

A little explanation here, I use the rand function (posted in my earlier post). I call randomize timer on form load, and have a command button that populates the listbox with the random numbers. It works by first getting a random number, and storing it in the variable NewNum. It starts the for loop, and sets "LastNum" to the same number as NewNum. This keeps track of the last random number used. We add it to the listbox, and try to get a new random number. The do loop here, loops until the new random number is NOT equal to the last random number. It will keep calling the rand function until the new random number is not the same as the last one.

I'm sure you'll have a number of questions, and that's ok. That's why we are all here. So, here is the project.... play with it, modify it, use it.... understand it.

Thanks Comatose I will study it and get to know it. I am getting there.

The code u need:

Private Sub Command1_Click()
Randomize

Label1.Caption = Str(Int(Rnd * 49) + 1)
Label2.Caption = Str(Int(Rnd * 49) + 1)
Label3.Caption = Str(Int(Rnd * 49) + 1)
Label4.Caption = Str(Int(Rnd * 49) + 1)
Label5.Caption = Str(Int(Rnd * 49) + 1)
Label6.Caption = Str(Int(Rnd * 49) + 1)

If (Label1 = Label2) Or (Label1 = Label3) Or (Label1 = Label4) Or (Label1 = Label5) Or (Label1 = Label6) Then Label1.Caption = Str(Int(Rnd * 49) + 1)

If (Label2 = Label1) Or (Label2 = Label3) Or (Label2 = Label4) Or (Label2 = Label5) Or (Label2 = Label6) Then Label2.Caption = Str(Int(Rnd * 49) + 1)

If (Label3 = Label1) Or (Label3 = Label2) Or (Label1 = Label4) Or (Label3 = Label5) Or (Label3 = Label6) Then Label3.Caption = Str(Int(Rnd * 49) + 1)

If (Label4 = Label1) Or (Label4 = Label2) Or (Label1 = Label3) Or (Label4 = Label5) Or (Label4 = Label6) Then Label4.Caption = Str(Int(Rnd * 49) + 1)

If (Label5 = Label1) Or (Label5 = Label2) Or (Label1 = Label3) Or (Label5 = Label4) Or (Label5 = Label6) Then Label5.Caption = Str(Int(Rnd * 49) + 1)

If (Label6 = Label1) Or (Label6 = Label2) Or (Label1 = Label3) Or (Label6 = Label4) Or (Label6 = Label5) Then Label6.Caption = Str(Int(Rnd * 49) + 1)

End Sub

The code u need:

Private Sub Command1_Click()
Randomize

Label1.Caption = Str(Int(Rnd * 49) + 1)
Label2.Caption = Str(Int(Rnd * 49) + 1)
Label3.Caption = Str(Int(Rnd * 49) + 1)
Label4.Caption = Str(Int(Rnd * 49) + 1)
Label5.Caption = Str(Int(Rnd * 49) + 1)
Label6.Caption = Str(Int(Rnd * 49) + 1)

If (Label1 = Label2) Or (Label1 = Label3) Or (Label1 = Label4) Or (Label1 = Label5) Or (Label1 = Label6) Then Label1.Caption = Str(Int(Rnd * 49) + 1)

If (Label2 = Label1) Or (Label2 = Label3) Or (Label2 = Label4) Or (Label2 = Label5) Or (Label2 = Label6) Then Label2.Caption = Str(Int(Rnd * 49) + 1)

If (Label3 = Label1) Or (Label3 = Label2) Or (Label1 = Label4) Or (Label3 = Label5) Or (Label3 = Label6) Then Label3.Caption = Str(Int(Rnd * 49) + 1)

If (Label4 = Label1) Or (Label4 = Label2) Or (Label1 = Label3) Or (Label4 = Label5) Or (Label4 = Label6) Then Label4.Caption = Str(Int(Rnd * 49) + 1)

If (Label5 = Label1) Or (Label5 = Label2) Or (Label1 = Label3) Or (Label5 = Label4) Or (Label5 = Label6) Then Label5.Caption = Str(Int(Rnd * 49) + 1)

If (Label6 = Label1) Or (Label6 = Label2) Or (Label1 = Label3) Or (Label6 = Label4) Or (Label6 = Label5) Then Label6.Caption = Str(Int(Rnd * 49) + 1)

End Sub

Umm, You forgot the End If's... the code should be:

Private Sub Command1_Click()
Randomize

Label1.Caption = Str(Int(Rnd * 49) + 1)
Label2.Caption = Str(Int(Rnd * 49) + 1)
Label3.Caption = Str(Int(Rnd * 49) + 1)
Label4.Caption = Str(Int(Rnd * 49) + 1)
Label5.Caption = Str(Int(Rnd * 49) + 1)
Label6.Caption = Str(Int(Rnd * 49) + 1)

If (Label1 = Label2) Or (Label1 = Label3) Or (Label1 = Label4) Or (Label1 = Label5) Or (Label1 = Label6) Then Label1.Caption = Str(Int(Rnd * 49) + 1)
End If

If (Label2 = Label1) Or (Label2 = Label3) Or (Label2 = Label4) Or (Label2 = Label5) Or (Label2 = Label6) Then Label2.Caption = Str(Int(Rnd * 49) + 1)
End If

If (Label3 = Label1) Or (Label3 = Label2) Or (Label1 = Label4) Or (Label3 = Label5) Or (Label3 = Label6) Then Label3.Caption = Str(Int(Rnd * 49) + 1)
End If

If (Label4 = Label1) Or (Label4 = Label2) Or (Label1 = Label3) Or (Label4 = Label5) Or (Label4 = Label6) Then Label4.Caption = Str(Int(Rnd * 49) + 1)
End If

If (Label5 = Label1) Or (Label5 = Label2) Or (Label1 = Label3) Or (Label5 = Label4) Or (Label5 = Label6) Then Label5.Caption = Str(Int(Rnd * 49) + 1)
End If

If (Label6 = Label1) Or (Label6 = Label2) Or (Label1 = Label3) Or (Label6 = Label4) Or (Label6 = Label5) Then Label6.Caption = Str(Int(Rnd * 49) + 1)
End If

End Sub

Umm, You forgot the End If's... the code should be:

Private Sub Command1_Click()
Randomize

Label1.Caption = Str(Int(Rnd * 49) + 1)
Label2.Caption = Str(Int(Rnd * 49) + 1)
Label3.Caption = Str(Int(Rnd * 49) + 1)
Label4.Caption = Str(Int(Rnd * 49) + 1)
Label5.Caption = Str(Int(Rnd * 49) + 1)
Label6.Caption = Str(Int(Rnd * 49) + 1)

If (Label1 = Label2) Or (Label1 = Label3) Or (Label1 = Label4) Or (Label1 = Label5) Or (Label1 = Label6) Then Label1.Caption = Str(Int(Rnd * 49) + 1)
End If

If (Label2 = Label1) Or (Label2 = Label3) Or (Label2 = Label4) Or (Label2 = Label5) Or (Label2 = Label6) Then Label2.Caption = Str(Int(Rnd * 49) + 1)
End If

If (Label3 = Label1) Or (Label3 = Label2) Or (Label1 = Label4) Or (Label3 = Label5) Or (Label3 = Label6) Then Label3.Caption = Str(Int(Rnd * 49) + 1)
End If

If (Label4 = Label1) Or (Label4 = Label2) Or (Label1 = Label3) Or (Label4 = Label5) Or (Label4 = Label6) Then Label4.Caption = Str(Int(Rnd * 49) + 1)
End If

If (Label5 = Label1) Or (Label5 = Label2) Or (Label1 = Label3) Or (Label5 = Label4) Or (Label5 = Label6) Then Label5.Caption = Str(Int(Rnd * 49) + 1)
End If

If (Label6 = Label1) Or (Label6 = Label2) Or (Label1 = Label3) Or (Label6 = Label4) Or (Label6 = Label5) Then Label6.Caption = Str(Int(Rnd * 49) + 1)
End If

End Sub

The code doesnt work with the End If. Tried it myself as im studyin btec national in ict and when the end if's come into practice, you get the compile error: "End If without Block if" was thoughtful of you mattyb15, you pointed out errors that would usually happen within some coding that people would usually miss out.

So in this case, the End IF's are a no go

This thread is two years old.... please don't post to threads that are older than three months.

This thread is two years old.... please don't post to threads that are older than three months.

sorry, just thought itd be helpful to others as i know quite a few people have had this problem so i thought id post some info.

shorthand of the vb code as follows:

Randomize

lblNumber1.Caption = (Int(Rnd * 49) + 1)
lblNumber2.Caption = (Int(Rnd * 49) + 1)
lblNumber3.Caption = (Int(Rnd * 49) + 1)
lblNumber4.Caption = (Int(Rnd * 49) + 1)
lblNumber5.Caption = (Int(Rnd * 49) + 1)
lblNumber6.Caption = (Int(Rnd * 49) + 1)

If (lblNumber1 = lblNumber#) Then lblNumber1.Caption = Str(Int(Rnd * 49) + 1)
If (lblNumber2 = lblNumber#) Then lblNumber2.Caption = Str(Int(Rnd * 49) + 1)
If (lblNumber3 = lblNumber#) Then lblNumber3.Caption = Str(Int(Rnd * 49) + 1)
If (lblNumber4 = lblNumber#) Then lblNumber4.Caption = Str(Int(Rnd * 49) + 1)
If (lblNumber5 = lblNumber#) Then lblNumber5.Caption = Str(Int(Rnd * 49) + 1)
If (lblNumber6 = lblNumber#) Then lblNumber6.Caption = Str(Int(Rnd * 49) + 1)

End Sub
-----------------------
EDIT
IT WORKS

There are rather a lot of complicated replies, when the solution is fairly simple...put all your numbers in a collection, select a number randomly from the collection..remove that from the collection. A duplicate is therefore impossible
the principle is:

(declarations)
public numarray as new collection
public selectednum(1 to 5) as integer

store 50 numbers
for I = 1 to 50
next I

pick the numbers

``````sequence=1
Do Until numberarray.Count = 0
randnum = Int((numberarray.Count * Rnd) + 1)
selectednum(sequence)=numberarray(randnum))
numberarray.Remove randnum
sequence = sequence + 1
sequence > 5 Then Exit Do
Loop``````

if you require more than 5 numbers change 'sequence > 5'

Dude... did you not read my post about the thread being old.... give it a rest people.

One for comatose...

If it should not be replied to, remove it.
otherwise give it a rest yourself.

commented: Responding to threads older than 3 months is poor etiquette. Get with the program. -2

btw guys, turned out i made an error in the code. finally have the full code so no "0" or blanks appear in the label (if using labels)

here is the correct code below -->

``````Dim Number As Integer

Randomize
End Sub

Private Sub cmdRun_Click()
lblNumber1.Caption = (Int(Rnd * 49) + 1)
lblNumber2.Caption = (Int(Rnd * 49) + 1)
lblNumber3.Caption = (Int(Rnd * 49) + 1)
lblNumber4.Caption = (Int(Rnd * 49) + 1)
lblNumber5.Caption = (Int(Rnd * 49) + 1)
lblNumber6.Caption = (Int(Rnd * 49) + 1)

If (lblNumber1 = lblNumber#) Then lblNumber1.Caption = (Int(Rnd * 49) + 1)
If (lblNumber2 = lblNumber#) Then lblNumber2.Caption = (Int(Rnd * 49) + 1)
If (lblNumber3 = lblNumber#) Then lblNumber3.Caption = (Int(Rnd * 49) + 1)
If (lblNumber4 = lblNumber#) Then lblNumber4.Caption = (Int(Rnd * 49) + 1)
If (lblNumber5 = lblNumber#) Then lblNumber5.Caption = (Int(Rnd * 49) + 1)
If (lblNumber6 = lblNumber#) Then lblNumber6.Caption = (Int(Rnd * 49) + 1)

End Sub``````

here is how i got sorting to work. im doing a adv. higher project for computing and am doing a lottery program. My code may be different from what u are looking for as when i sort the numbers i don't sort the bonus number but every other number is sorted.

the following code is in procedures also:

Private Sub sorting_random_numbers(ByRef outer As Integer, inner As Integer, number_player() As Integer, temp As Integer)

'sorting randomly generated numbers

``````For outer = 0 To 4
For inner = 0 To 4
If number_player(outer) < number_player(inner) Then
temp = number_player(outer)
number_player(outer) = number_player(inner)
number_player(inner) = temp
End If
Next inner
Next outer``````

You could use this code and try and change the variable names to try and get it into context with your program. If you use procedures u have to call it at the begining of your program underneath your declared variables.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.