# Thread: [2005] How to stop duplicate random numbers?

1. ## [2005] How to stop duplicate random numbers?

Hi, I have (as you probably know) been messing around with random numbers alot recently. I just quickly came up with a Lotto Number Generator there but I need a way to stiop geting duplicate results eg. I want
4, 7, 16, 42, 34, 9 instead of
4, 7, 7, 3, 46, 9.

I have a feeling this involves loop and i am not veryu good with loops so some assistance would be great tanks

2. ## Re: [2005] How to stop duplicate random numbers?

Anyone got any ideas? i need this quite soon!

3. ## Re: [2005] How to stop duplicate random numbers?

Use the Random class instead of Rnd(). You can also add the original items to an arraylist, then each number that is picked, just remove the item from the arraylist so it cant pick it again. This would require you to call Random.Next with the count of the items instead of using "50" hard coded....

4. ## Re: [2005] How to stop duplicate random numbers?

VB Code:
```Dim I, number As Integer
For I = 0 To whatever
Do
number = CInt(Rnd() * whatever)
Next```

5. ## Re: [2005] How to stop duplicate random numbers?

You deleted your post and added it under mine mrcrash, so now its out of order

Below is an example of using an arraylist, so you can just use the .Remove or .RemoveAt method in order to remove the item from the list so it isnt picked again, thus not giving you duplicates. It also uses the Random class instead of Rnd()...
VB Code:
```'initializes arraylist with all numbers
Dim LottoNumbers As New ArrayList
For I As Integer = 0 To 49 Step 1
Next
Dim MyRandom As New Random
'array to hold the picked numbers
Dim Picked(5) As Integer
For I As Integer = 0 To 5 Step 1
'gets next random index using the random class
Dim PickedIndex As Integer = MyRandom.Next(0, LottoNumbers.Count - 1)
'sets value in picked array to the value at the picked index
Picked(i) = CInt(LottoNumbers(PickedIndex))
'removes index from the list so it cant be picked again
LottoNumbers.RemoveAt(PickedIndex)
Next
'just displays results
Dim ReturnString As String
For Each Number As Integer In Picked
ReturnString &= Number.ToString & " "
Next
MessageBox.Show(ReturnString)```

6. ## Re: [2005] How to stop duplicate random numbers?

Sorry....
I deleted it because the code i gave was wrong!

7. ## Re: [2005] How to stop duplicate random numbers?

Error 1 Class 'System.Random' cannot be indexed because it has no default property. C:\Documents and Settings\Compaq_Owner\Local Settings\Application Data\Temporary Projects\WindowsApplication1\Form1.vb 36 31 WindowsApplication1

is the error I get with that

8. ## Re: [2005] How to stop duplicate random numbers?

no, srry that was for mrcrashes code, gigemboy, even with that code (except without the display method because I already have one) I still get numbers the same... that time I got
17, 1, 3, 26, 46, 26

9. ## Re: [2005] How to stop duplicate random numbers?

Originally Posted by Chael
no, srry that was for mrcrashes code, gigemboy, even with that code (except without the display method because I already have one) I still get numbers the same... that time I got
17, 1, 3, 26, 46, 26
Hi Chael,

I saw the code of gigemboy as well and i changed a little bit so when you click a button your numbers will be shown in a label, but the most important the code is working!
Try this,

VB Code:
```Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim MyNumbers As New ArrayList
For I As Integer = 0 To 49 Step 1
Next
Dim MyRandom As New Random
'array to hold the picked numbers
Dim Picked(5) As Integer
For I As Integer = 0 To 5 Step 1
'gets next random index using the random class
Dim PickedIndex As Integer = MyRandom.Next(0, MyNumbers.Count - 1)
Picked(i) = CInt(MyNumbers(PickedIndex))
'removes index from the list so it cant be picked again
MyNumbers.RemoveAt(PickedIndex)
Next
'just displays results
Dim ReturnString As String
For Each Number As Integer In Picked
ReturnString &= Number.ToString & " "
Next
Label1.Text = (ReturnString)
End Sub```

Wkr,

sparrow1

10. ## Re: [2005] How to stop duplicate random numbers?

I dont know how you got duplicates still, since the value at the picked index is removed as soon as it is assigned to the picked numbers, so that number is removed from the arraylist on the next loop, so there is no way to get that same number again, as Sparrow has even tested...

11. ## Re: [2005] How to stop duplicate random numbers?

I doubt that it's really any different to what Gig posted but I made a Codebank submission on this some time ago. See my signature for a link.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•