-
Jul 16th, 2018, 09:40 AM
#1
Thread Starter
Member
Generate random numbers without duplicates
How can I generate random numbers without having duplicates?
Here is my interface:
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.
-
Jul 16th, 2018, 10:25 AM
#2
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.
-
Jul 16th, 2018, 10:40 AM
#3
Re: Generate random numbers without duplicates
Originally Posted by dday9
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:
Dim uniques As IEnumerable(Of Integer) = Enumerable.Range(1, 41). OrderBy(Function(n) r.NextDouble()). Take(6)
-
Jul 16th, 2018, 11:18 AM
#4
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.
-
Jul 16th, 2018, 11:28 AM
#5
Thread Starter
Member
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.
-
Jul 16th, 2018, 11:41 AM
#6
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.
-
Jul 16th, 2018, 12:03 PM
#7
Thread Starter
Member
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
-
Jul 16th, 2018, 12:19 PM
#8
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)
-
Jul 16th, 2018, 01:00 PM
#9
Thread Starter
Member
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!!
-
Jul 16th, 2018, 01:08 PM
#10
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.
-
Jul 17th, 2018, 12:36 PM
#11
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.
-
Jul 17th, 2018, 02:41 PM
#12
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.
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
|