-
Jun 6th, 2017, 10:06 AM
#1
Thread Starter
Lively Member
Help draw twelve names without duplicate from txt file(code help)
I have a list of names in a txt files and i would like to have a code to draw randomly 12 names(or lines) without duplicate and put each in 12 combobox ( each combobox have a name(or line)
I am a beginner ,thank you to put a max of details.
-
Jun 6th, 2017, 10:24 AM
#2
Re: Help draw twelve names without duplicate from txt file(code help)
The process to pick random items with no duplicates varies a little bit based on circumstances, but here's one that works:
- Load every item in the file into a List.
- Create an instance of the Random class.
- Generate a random number between 0 and the list's Length -1.
- Remove that item from the list and add it to a different list.
- Repeat 3 and 4 until either the initial list is empty or the different list has 12 items.
- Now the "different" list is 12 random items with no duplicates.
Try it out. If any of the steps are confusing, ask about them.
This answer is wrong. You should be using TableAdapter and Dictionaries instead.
-
Jun 6th, 2017, 10:40 AM
#3
Thread Starter
Lively Member
Re: Help draw twelve names without duplicate from txt file(code help)
Originally Posted by Sitten Spynne
The process to pick random items with no duplicates varies a little bit based on circumstances, but here's one that works:
- Load every item in the file into a List.
- Create an instance of the Random class.
- Generate a random number between 0 and the list's Length -1.
- Remove that item from the list and add it to a different list.
- Repeat 3 and 4 until either the initial list is empty or the different list has 12 items.
- Now the "different" list is 12 random items with no duplicates.
Try it out. If any of the steps are confusing, ask about them.
OK i understood but i need a code for the step 3 and step 4 .
please what mean random class.is a form?
thank for your smart answer
-
Jun 6th, 2017, 11:21 AM
#4
Re: Help draw twelve names without duplicate from txt file(code help)
Here is an example of demonstrating Sitten's instructions:
Code:
'Declare a new random object at the top-most level
Dim r As New Random
'Download the file into a collection
Dim lines() As String = IO.File.ReadAllLines("names.txt")
'Step1: Create a range of 0 to the length of the array
'Step2: Randomly order the array
'Step3: Take only 12 items from range
'Step4: Return the items from the lines array that match the index of the taken indices
Dim names() As String = (From i As Integer In enumerable.Range(0, lines.Length - 1) Order By r.Next() Take(12) Select lines(i)).ToArray()
Fiddle: https://dotnetfiddle.net/ynkjmC
-
Jun 6th, 2017, 11:37 AM
#5
Re: Help draw twelve names without duplicate from txt file(code help)
That LINQ can be a bit hard to understand. You can do the same thing with a loop and see the steps a bit more clearly:
Code:
'Assuming the lines are in a List(of String) called NameSet;
'Also assuming that there are more than 12 names in the set.
Dim selectionSet As New List(of String)
Dim rand As New Random
Dim selectedName As String
For x = 0 to 11
selectedName = NameSet(rand.Next(0,NameSet.Count))
selectionSet.Add(selectedName)
Next
That can be simplified by getting rid of selectedName, or can be simplified even further by using a single line LINQ statement as DDay showed. The loop can be easier when starting out, and will have superior performance, but either way will take less than a millisecond, so there isn't much difference.
My usual boring signature: Nothing
-
Jun 6th, 2017, 11:39 AM
#6
Re: Help draw twelve names without duplicate from txt file(code help)
Eh, I think that level of sophistication might lead to some interesting questions from whoever assigned this homework.
3-4 would look like this:
Code:
Dim rng As New Random()
Dim randomNumber As Integer = rng.Next(0, 10)
The Random class is used to generate random numbers. The Next() method, when used this way, will generate a number >= the first number and < the second number. That's a little weird, but the code above will generate a number between 0 and 9.
So let's say I have a list of 10 integers named '_items'. If I want to print three random items from the list, with possible duplicates:
Code:
Dim rng As New Random()
For i As Integer = 1 To 3
Dim randomIndex As Integer = rng.Next(0, _items.Count)
Dim randomItem As Integer = _items(randomIndex)
Console.WriteLine(randomItem)
Next
It's important to NEVER call 'New Random()' inside a loop. If you do that, for technical reasons, you'll very likely see it give you the same numbers over and over again.
So your basic skeleton should look like this:
Code:
Dim originalItems As new List(Of String)() ' I'm guessing String
' <Code to open the file and put all the items in _original items here>
Dim rng As New Random()
Dim randomItems As New List(Of String)()
For i As Integer = 1 To 12
Dim randomIndex As Integer = rng.Next(0, _originalItems.Count)
' <add the item at that index to randomItems>
' <remove the item at that index from originalItems>
Next
There's some blanks there for you to fill in, but if you get stuck or it doesn't work post what you tried and we'll explain what went wrong!
This answer is wrong. You should be using TableAdapter and Dictionaries instead.
-
Jun 6th, 2017, 02:41 PM
#7
Thread Starter
Lively Member
Re: Help draw twelve names without duplicate from txt file(code help)
OK i understand a bit your reasoning. so i make a beginning with my problem.so we have this
Code:
Dim lines() As String = IO.File.ReadAllLines(Application.StartupPath & "\DANfiles\charfighters.txt")' path of txt
Dim selectionSet As New List(Of String)
Dim rand As New Random
Dim selectedName As String
For x = 0 To 11
selectedName = lines(rand.Next(0, lines.Count))
selectionSet.Add(selectedName)
Next
' i need put all selected names each one in a combobox different
' ex mark into combobox1, helen into combobox2,ect... to 12 combobox
' must i use a listbox?
Could you add some lines to my code to complete this project. thank you
-
Jun 6th, 2017, 02:46 PM
#8
Re: Help draw twelve names without duplicate from txt file(code help)
That doesn't make any sense. Putting all the names in one combobox (or a listbox) makes sense, but putting each name into a different combobox doesn't make any sense. The only thing you can do with a combobox is to choose an item once you drop it down. In this case, there will only be the one item to choose, so why bother dropping it down?
So, something is missing from that explanation. Is there other stuff that goes into the combobox? If so, what is it? Even if there is, I'd be wanting to put the names into labels above or beside the comboboxes.
My usual boring signature: Nothing
-
Jun 6th, 2017, 02:50 PM
#9
Thread Starter
Lively Member
Re: Help draw twelve names without duplicate from txt file(code help)
Sorry, i just want to catch them separetely , i would have to say a label.
-
Jun 6th, 2017, 02:54 PM
#10
Re: Help draw twelve names without duplicate from txt file(code help)
Yeah, a label would make more sense. A listbox would allow you to see them all, and sort them all (you can actually just call .Sort on selectionSet after it has been filled, if you want to Sort), and a listbox would be easier to extend. Putting twelve labels on the screen and assigning a different name to each one is possible, but becomes a chore if there is ever a thirteenth label added, or more. So, if you know it will always be 12, then labels do make sense, but if the number has any chance of changing, a Listbox would be easier.
My usual boring signature: Nothing
-
Jun 6th, 2017, 03:07 PM
#11
Thread Starter
Lively Member
Re: Help draw twelve names without duplicate from txt file(code help)
Indeed ,the number stays 12 but 1.how retrieve my selectedname in a listbox 2 how transfer in a label( easy but i am beginner) thank you for you answer!
-
Jun 6th, 2017, 03:12 PM
#12
Re: Help draw twelve names without duplicate from txt file(code help)
You can get the currently selected item in a ListBox by getting the Text property, from there simply assign the Text property of the ListBox to the Text property of the Label like this:
Code:
Label1.Text = ListBox1.Text
-
Jun 6th, 2017, 03:19 PM
#13
Thread Starter
Lively Member
Re: Help draw twelve names without duplicate from txt file(code help)
Originally Posted by dday9
You can get the currently selected item in a ListBox by getting the Text property, from there simply assign the Text property of the ListBox to the Text property of the Label like this:
Code:
Label1.Text = ListBox1.Text
OK but it is selectionset in a listbox,my problem is.( the list of random selection(12 names) in the listbox)how code it?
-
Jun 6th, 2017, 04:17 PM
#14
Re: Help draw twelve names without duplicate from txt file(code help)
If you were to implement my code then it would look a little bit like this:
Code:
Public Class Form1
Private r As New Random
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
Dim path As String = IO.Path.Combine(Application.StartupPath, "DANfiles", "charfighters.txt")
Dim lines() As String = IO.File.ReadAllLines(path)
Dim names() As String = (From i As Integer In enumerable.Range(0, lines.Length - 1) Order By r.Next() Take(12) Select lines(i)).ToArray()
ListBox1.Items.AddRange(names)
End Sub
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ListBox1.SelectedIndexChanged
If ListBox1.SelectedIndex <> -1 Then
Label1.Text = ListBox1.Text
Else
Label1.Text = "N/A"
End If
End Sub
End Class
Tags for this Thread
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
|