Results 1 to 11 of 11

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

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2006
    Posts
    60

    [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. #2

    Thread Starter
    Member
    Join Date
    Mar 2006
    Posts
    60

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

    Anyone got any ideas? i need this quite soon!

  3. #3
    "The" RedHeadedLefty
    Join Date
    Aug 2005
    Location
    College Station, TX Preferred Nickname: Gig Current Mood: Just Peachy Turnons: String Manipulation
    Posts
    4,495

    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.

  4. #4
    Member
    Join Date
    Nov 2005
    Location
    Kent
    Posts
    40

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

    VB Code:
    1. Dim I, number As Integer
    2.         Dim asked() As Boolean
    3.  
    4.         For I = 0 To whatever
    5.             Do
    6.                 number = CInt(Rnd() * whatever)
    7.  
    8.             Loop Until asked(number) = False
    9.             asked(number) = True
    10.         Next
    It's only easy if you know!
    VB.NET2003/1.1
    VB2005/2.0

  5. #5
    "The" RedHeadedLefty
    Join Date
    Aug 2005
    Location
    College Station, TX Preferred Nickname: Gig Current Mood: Just Peachy Turnons: String Manipulation
    Posts
    4,495

    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:
    1. 'initializes arraylist with all numbers
    2.         Dim LottoNumbers As New ArrayList
    3.         For I As Integer = 0 To 49 Step 1
    4.             LottoNumbers.Add(I + 1)
    5.         Next
    6.         Dim MyRandom As New Random
    7.         'array to hold the picked numbers
    8.         Dim Picked(5) As Integer
    9.         For I As Integer = 0 To 5 Step 1
    10.             'gets next random index using the random class
    11.             Dim PickedIndex As Integer = MyRandom.Next(0, LottoNumbers.Count - 1)
    12.             'sets value in picked array to the value at the picked index
    13.             Picked(i) = CInt(LottoNumbers(PickedIndex))
    14.             'removes index from the list so it cant be picked again
    15.             LottoNumbers.RemoveAt(PickedIndex)
    16.         Next
    17.         'just displays results
    18.         Dim ReturnString As String
    19.         For Each Number As Integer In Picked
    20.             ReturnString &= Number.ToString & " "
    21.         Next
    22.         MessageBox.Show(ReturnString)
    Last edited by gigemboy; Apr 9th, 2006 at 04:59 AM.

  6. #6
    Member
    Join Date
    Nov 2005
    Location
    Kent
    Posts
    40

    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

  7. #7

    Thread Starter
    Member
    Join Date
    Mar 2006
    Posts
    60

    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. #8

    Thread Starter
    Member
    Join Date
    Mar 2006
    Posts
    60

    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. #9
    PowerPoster sparrow1's Avatar
    Join Date
    May 2005
    Location
    Globetrotter
    Posts
    2,817

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

    Quote 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:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         Dim MyNumbers As New ArrayList
    3.         For I As Integer = 0 To 49 Step 1
    4.             MyNumbers.Add(I + 1)
    5.         Next
    6.         Dim MyRandom As New Random
    7.         'array to hold the picked numbers
    8.         Dim Picked(5) As Integer
    9.         For I As Integer = 0 To 5 Step 1
    10.             'gets next random index using the random class
    11.             Dim PickedIndex As Integer = MyRandom.Next(0, MyNumbers.Count - 1)
    12.             Picked(i) = CInt(MyNumbers(PickedIndex))
    13.             'removes index from the list so it cant be picked again
    14.             MyNumbers.RemoveAt(PickedIndex)
    15.         Next
    16.         'just displays results
    17.         Dim ReturnString As String
    18.         For Each Number As Integer In Picked
    19.             ReturnString &= Number.ToString & " "
    20.         Next
    21.         Label1.Text = (ReturnString)
    22.     End Sub

    Wkr,

    sparrow1
    Wkr,
    sparrow1

    If I helped you, don't forget to Rate my post. Thank you

    I'm using Visual Studio.Net 2003 and
    2005
    How to learn VB.Net Create setup with VB 2005 Drawing for beginners VB.Net Tutorials GDI+ Tutorials
    Video's for beginners

  10. #10
    "The" RedHeadedLefty
    Join Date
    Aug 2005
    Location
    College Station, TX Preferred Nickname: Gig Current Mood: Just Peachy Turnons: String Manipulation
    Posts
    4,495

    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. #11
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,439

    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
  •  



Click Here to Expand Forum to Full Width