raymond
raymond

Reputation: 17

random numbers without repeating

Actually I've already found a working loop here. but the problem is i don't know how to use it properly (noob coder here :D). what i want to do is display a random question each click until all 20 of them has been used up. but my code below restarts each click and i cant make the counter work. i hope someone can help me :)

    Dim questions() As String = System.IO.File.ReadAllLines(filename)
    Dim xGenerator As System.Random = New System.Random()
    Dim xTemp As Integer = 0
    Dim xRndNo As New List(Of Integer)
    While Not xRndNo.Count = 20
        xTemp = xGenerator.Next(0, 20)
        If xRndNo.Contains(xTemp) Then
            Continue While
        Else
            xRndNo.Add(xTemp)
            Label1.Text = "question no.:" & xTemp
            Label2.Text = questions(xTemp)
            Label3.text = xRndNo.Count 'a hidden counter 
        End If
    End While
    objStreamReader.Close()

Upvotes: 1

Views: 238

Answers (1)

Enigmativity
Enigmativity

Reputation: 117175

I'm not sure exactly where you are getting stuck, but this might help.

Write your code like this:

Dim questions() As String = System.IO.File.ReadAllLines(filename)
Dim xGenerator As System.Random = New System.Random()
Dim randomQuestions = _
    questions _
        .OrderBy(Function (x) xGenerator.Next()) _
        .ToArray()

Now you just have to keep track of a single counter to index randomQuestions and then update your text fields and increment it each time a person answers a question.

Upvotes: 1

Related Questions