Dim aryName As String() = Nothing
aryName = File.ReadAllLines(Application.StartupPath & "\Random\fnames.txt")
Dim randomWords As New List(Of String)
For i = 0 To aryName.Length - 1
If randomWords.Contains(aryName(i)) = False Then
randomWords.Add(aryName(i))
End If
Next
Dim random As New Random
Label2.Text = (randomWords(random.Next(0, randomWords.Count - 1)).ToString)
that code is supposed to take words from a txt file and then put them into a label, only problem is; the words are sometimes repeated. I only want each word to appear once but instead they appear multiple times.
You’re using the entire array of words every time you check for a random word. My understanding of
random.Next()isn’t that it uses a different random number from 0-n each time it is called, but generates a new number from 0-n, which might repeat before n times has occurred.To fix this, adjust your array each time you use a word – move the one it found to the end and adjust your range-1.