|
-
Apr 9th, 2006, 03:47 AM
#1
Thread Starter
Member
[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
-
Apr 9th, 2006, 04:18 AM
#2
Thread Starter
Member
Re: [2005] How to stop duplicate random numbers?
Anyone got any ideas? i need this quite soon!
-
Apr 9th, 2006, 04:23 AM
#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....
Last edited by gigemboy; Apr 9th, 2006 at 04:34 AM.
-
Apr 9th, 2006, 04:36 AM
#4
Member
Re: [2005] How to stop duplicate random numbers?
VB Code:
Dim I, number As Integer
Dim asked() As Boolean
For I = 0 To whatever
Do
number = CInt(Rnd() * whatever)
Loop Until asked(number) = False
asked(number) = True
Next
It's only easy if you know!
VB.NET2003/1.1
VB2005/2.0
-
Apr 9th, 2006, 04:49 AM
#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
LottoNumbers.Add(I + 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)
Last edited by gigemboy; Apr 9th, 2006 at 04:59 AM.
-
Apr 9th, 2006, 04:52 AM
#6
Member
Re: [2005] How to stop duplicate random numbers?
Sorry....
I deleted it because the code i gave was wrong!
It's only easy if you know!
VB.NET2003/1.1
VB2005/2.0
-
Apr 9th, 2006, 05:14 AM
#7
Thread Starter
Member
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
-
Apr 9th, 2006, 05:19 AM
#8
Thread Starter
Member
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
-
Apr 9th, 2006, 06:16 AM
#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
MyNumbers.Add(I + 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
-
Apr 9th, 2006, 06:21 AM
#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...
-
Apr 9th, 2006, 09:50 AM
#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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|