Results 1 to 12 of 12

Thread: Generate random numbers without duplicates

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2017
    Posts
    33

    Generate random numbers without duplicates

    How can I generate random numbers without having duplicates?
    Here is my interface:

    Name:  Eliminate Duplicates.JPG
Views: 2640
Size:  16.9 KB

    Here is my code:

    Code:
    Dim FirstNumber As Integer = Convert.ToInt32(txtFirstNumber.Text)
    Dim SecondNumber As Integer = Convert.ToInt32(txtSecondNumber.Text)
    Dim Rand As Random = New Random
    
                txtGBall1.Text = Rand.Next(FirstNumber, SecondNumber)
                txtGBall2.Text = Rand.Next(FirstNumber, SecondNumber)
                txtGBall3.Text = Rand.Next(FirstNumber, SecondNumber)
                txtGBall4.Text = Rand.Next(FirstNumber, SecondNumber)
                txtGBall5.Text = Rand.Next(FirstNumber, SecondNumber)
                txtGBall6.Text = Rand.Next(FirstNumber, SecondNumber)
            End If
    Note: I`m aware I can be using listboxes to display a given set of random numbers straight away, without having separate controls for each number, but due to my preferences and needs do I require such interface.

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,711

    Re: Generate random numbers without duplicates

    Try:
    Code:
    Dim uniques As IEnumerable(Of Integer) = Enumerable.Range(1, 6).Select(Function(n) r.Next(1, 41))
    Where r is a Random object.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Generate random numbers without duplicates

    Quote Originally Posted by dday9 View Post
    Try:
    Code:
    Dim uniques As IEnumerable(Of Integer) = Enumerable.Range(1, 6).Select(Function(n) r.Next(1, 41))
    Where r is a Random object.
    That won't guarantee uniqueness. It will just return any 6 random numbers in that range. This will return 6 unique random numbers in that range:
    vb.net Code:
    1. Dim uniques As IEnumerable(Of Integer) = Enumerable.Range(1, 41).
    2.                                                     OrderBy(Function(n) r.NextDouble()).
    3.                                                     Take(6)

  4. #4
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,711

    Re: Generate random numbers without duplicates

    Thank you. I wrote it on the fly and free-typed it, so I didn't give it 100% thought like I should've.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  5. #5

    Thread Starter
    Member
    Join Date
    Feb 2017
    Posts
    33

    Re: Generate random numbers without duplicates

    When triggered, it only returns System.Linq.OrderedEnumerable`2[System.Int32,System.Double], it doesn`t return any actual random number.

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

    Re: Generate random numbers without duplicates

    That's the way LINQ works. What you have is an IEnumerable, so you can enumerate it, i.e. run a For Each loop over it. If you want the numbers in an array or List then call ToArray to ToList on it.

  7. #7

    Thread Starter
    Member
    Join Date
    Feb 2017
    Posts
    33

    Re: Generate random numbers without duplicates

    The For Each loop worked, but it`s still generating duplicates.

    Here is my updated code:

    Code:
    Dim FirstNumber As Integer = Convert.ToInt32(txtFirstNumber.Text)
    Dim SecondNumber As Integer = Convert.ToInt32(txtSecondNumber.Text)
    Dim Rand As Random = New Random
    
    
    Dim uniques As IEnumerable(Of Integer) = Enumerable.Range(FirstNumber, SecondNumber).OrderBy(Function(n) Rand.NextDouble())
    
                For Each item1 As Integer In uniques
                    txtGBall1.Text = item1.ToString()
                Next
    
                For Each item2 As Integer In uniques
                    txtGBall2.Text = item2.ToString()
                Next
    
                For Each item3 As Integer In uniques
                    txtGBall3.Text = item3.ToString()
                Next
    
                For Each item4 As Integer In uniques
                    txtGBall4.Text = item4.ToString()
                Next
    
                For Each item5 As Integer In uniques
                    txtGBall5.Text = item5.ToString()
                Next
    
                For Each item6 As Integer In uniques
                    txtGBall6.Text = item6.ToString()
                Next

  8. #8
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

    Re: Generate random numbers without duplicates

    It isn't necessarily generating duplicates, but your code is displaying duplicates because it is re-using all of the items for each of your textboxes.

    A For-Each loop performs the tasks inside the loop for every item in the list, so this code:
    Code:
                For Each item1 As Integer In uniques
                    txtGBall1.Text = item1.ToString()
                Next
    ...puts the first item into txtGBall1.Text, then it puts the second item in there, and so on until the last item (so the overall effect is that after the loop txtGBall1.Text will contain the last item in uniques). As you use the same code (just with different variable names) for all of the textboxes, they may well contain the same item.

    As you only want one item in each textbox, it doesn't make sense to use a loop around each textbox. It would be better to use ToArray or ToList as suggested before, then assign the values to each textbox, eg:
    Code:
      txtGBall1.Text = theArrayOrList(0)
      txtGBall2.Text = theArrayOrList(1)

  9. #9

    Thread Starter
    Member
    Join Date
    Feb 2017
    Posts
    33

    Re: Generate random numbers without duplicates

    Okay, I fixed the code. Now I`m getting unique numbers.

    Code:
    Dim uniques As IEnumerable(Of Integer) = Enumerable.Range(FirstNumber, SecondNumber).OrderBy(Function(n) Rand.NextDouble())
    Dim array() As Integer = uniques.ToArray
    
                For Each item As Integer In uniques
                    txtGBall1.Text = array(0)
                    txtGBall2.Text = array(1)
                    txtGBall3.Text = array(2)
                    txtGBall4.Text = array(3)
                    txtGBall5.Text = array(4)
                    txtGBall6.Text = array(5)
                Next
    Thank you!!

  10. #10
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

    Re: Generate random numbers without duplicates

    You shouldn't have the For Each loop, because it isn't doing anything worthwhile - it just runs those lines multiple times, giving the same end result as just running it once (because array doesn't change inside that loop). Having the loop increases the amount of code and the amount of time it takes to run.

  11. #11
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,040

    Re: Generate random numbers without duplicates

    Hi,

    another way would be to create a Database table with numbers 1 to 40, and then execute a query

    here a sample for Access Database..
    Code:
    SELECT TOP 6 tbl_Numbers.L_ID, tbl_Numbers.L_Text
    FROM tbl_Numbers
    ORDER BY rnd(ISNULL(tbl_Numbers.L_ID) * 0 +1);
    this will allways return 6 unique numbers

    regards
    Chris
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  12. #12
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,711

    Re: Generate random numbers without duplicates

    ChrisE - The method that you're proposing is essentially the same as what JMcIlhinney proposed, the difference is that JMcIlhinney's proposal uses in-memory data and LINQ to execute the query whereas your proposal uses external memory data and SQL to execute the query.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

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