Results 1 to 6 of 6

Thread: Get Random value with no duplicate

  1. #1

    Thread Starter
    Lively Member nilesh16782's Avatar
    Join Date
    Feb 2007
    Location
    India
    Posts
    104

    Get Random value with no duplicate

    hi... i use this code in which i want different value of i (no duplicate). . but this code return some duplicate value of i...
    ----------------------------
    Dim r As New Random
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    dim i as Integer
    For i = 1 To 5
    MsgBox(r.Next(0, 5))
    Next
    End Sub
    '----------------------

  2. #2
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: Get Random value with no duplicate

    Of course it will produce duplicates - the random class generates random numbers, nothing more.

    If you want to prevent duplicates you need some method of keeping track of whether a number has already been selected. In simple cases you may just want to declare an array of booleans with the upper limit = the upper limit of your random range, and then every time you pick your random number check that element in the array to see if its set to true, if it is then pick again, if not use that number and set the corresponding element of the array to true so it can't get picked again.

  3. #3
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: Get Random value with no duplicate

    An easier way is to fill a list or array with the numbers you want to select a random number from. Then, you select an element from that list at random. Once you selected it, you remove that element from the list, and then it cannot be selected a second time.

    To select a random element from a list, simply generate a random index (between 0 and the upper bound of the list), and extract the value of the list at that index.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Get Random value with no duplicate

    Follow the Codebank link in my signature and check my two threads on this topic. There is one on Unique Random Selections and one on Randomising a List. Which technique you choose depends on the specifics of the project.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    New Member
    Join Date
    Aug 2010
    Posts
    1

    Re: Get Random value with no duplicate

    Use HashSet (Of T) instead of arraylist.
    HashSet(Of T) keeps out duplicated values.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Get Random value with no duplicate

    Quote Originally Posted by junnybol View Post
    Use HashSet (Of T) instead of arraylist.
    HashSet(Of T) keeps out duplicated values.
    A HashSet will prevent you adding duplicates to the collection but it doesn't prevent you from generating duplicate values and trying to add them. You could keep generating numbers if Add returns False but that's rather inefficient, especially as the number of possible values increases. If you can ensure that you never generate a duplicate then whether you use a HashSet or List or whatever isn't really relevant.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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