hi!
I am quite puzzled here. I have a program inwhich when the user clicks on the command button, a random line should appear but only once.

I am storing each line with different variables.

Right now i have the random line coming up but it is repeating itself.
For instance, when i click on the button, line 1 appear, then line 4, and them line 1 again. How can i stop the line 1 to show again?
Thank you

Recommended Answers

All 3 Replies

Steps:

1. Add lines into the List (collection framework)
2. Get line at random index
3. Draw a line
4. Remove that line from the list.
5. Go to step 2 till count is >0

my code for the moment is:

Function LoadNewQuestion() As String 'To lad a phrase
        Dim ThisNewQuestion As String
        Dim LoadedQuestionArray(10) As String
        Dim QuestionCount As Integer = 0
dim mycount2 as string
    
 FileOpen(1, "G:\Qu.txt", OpenMode.Input) 'open the file
Start:
                While Not EOF(1) 'end of file
                    QuestionCount += 1
            LoadedQuestionArray(QuestionCount) = (LineInput(1))


                    'the phrase is a random loaded phrase
                End While
                FileClose(1) 'close file
        ThisNewQuestion = LoadedQuestionArray(RandomNum(QuestionCount))
        mycount2 = LoadedQuestionArray(RandomNum(QuestionCount - 1))
        If mycount2 = ThisNewQuestion Then
            GoTo start


        Else : Label1.Text = ThisNewQuestion

        End If

        Return ThisNewQuestion 'returning the function
    End Function
    Function RandomNum(ByVal maxnum As Integer) As Integer
        Randomize()
        Return Math.Ceiling(Rnd() * maxnum)

    End Function

End Class

an error say bad file name or number

Thank you

Have a look,

...
 Dim lines As List(Of String) = System.IO.File.ReadAllLines("c:\xyz\file.txt").ToList()

        Dim rnd As New Random
        Dim ind As Integer
        Randomize()
        While (lines.Count <> 0)
            ind = rnd.Next(0, lines.Count)
            Console.WriteLine(lines(ind))
            lines.RemoveAt(ind)
        End While
...
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.