Gamehakerz13
Gamehakerz13

Reputation: 21

How to make Random string Output from string list Not Repeat VB.Net

i have made a multiple choice quiz for my first assignment yr 11 and my randomizer is outputting some of the questions multiple times, which id like to fix but i cant figure it out.

Here are my Declarations

 Dim questions As New List(Of String)
 Dim answers As New List(Of String)
 Dim check As New List(Of String)
 Dim gotQA As Boolean = False
 Dim currentQuestion As Integer = -1
 Dim randomQuestions As Integer = 0
 Dim random As Random = New Random()
 Dim rightQuestions As New List(Of String)
 Dim wrongQuestions As New List(Of String)
 Public Shared Carrytof3 As String

Here's the next question button code (the validator as well)

Private Sub CorrectAns()
    Dim ans As String = TextBox1.Text
    check.Add(Label1.Text)
    If (ans = answers(currentQuestion)) Then
        rightQuestions.Add(currentQuestion)
        If rightQuestions.Count() = 0 Then
            Dim percent As Integer = (rightQuestions.Count() / 20) * 100
            Carrytof3 = (Form1.fname & " " & Form1.lname & " " & "That's Terrible " & "You scored " & rightQuestions.Count() & " Out of 20" & vbNewLine & "Thats " & percent & "%")
        End If
        If rightQuestions.Count() >= 0 Then
            If rightQuestions.Count() <= 4 Then
                Dim percent As Integer = (rightQuestions.Count() / 20) * 100
                Carrytof3 = (Form1.fname & " " & Form1.lname & " " & "BAD LUCK!, Try Again " & "You scored " & rightQuestions.Count() & " Out of 20" & vbNewLine & "Thats " & percent & "%")
            End If
        End If
        If rightQuestions.Count() >= 5 Then
            If rightQuestions.Count() <= 10 Then
                Dim percent As Integer = (rightQuestions.Count() / 20) * 100
                Carrytof3 = (Form1.fname & " " & Form1.lname & " " & "Good Effort You can do better " & "You scored " & rightQuestions.Count() & " Out of 20" & vbNewLine & "Thats " & percent & "%")
            End If
        End If
        If rightQuestions.Count() >= 11 Then
            If rightQuestions.Count() <= 19 Then
                Dim percent As Integer = (rightQuestions.Count() / 20) * 100
                Carrytof3 = (Form1.fname & " " & Form1.lname & " " & "Well Done Nearly There " & "You scored " & rightQuestions.Count() & " Out of 20" & vbNewLine & "Thats " & percent & "%")
            End If
        End If
        If rightQuestions.Count() = 20 Then
            Dim percent As Integer = (rightQuestions.Count() / 20) * 100
            Carrytof3 = (Form1.fname & " " & Form1.lname & " " & "Excellent Job " & "You scored " & rightQuestions.Count() & " Out of 20" & vbNewLine & "Thats " & percent & "%")
        End If
    Else : wrongQuestions.Add(currentQuestion)
    End If
    TextBox1.Text = ""
    If (Not randomQuestions >= 20) Then
        Dim tempR As Integer = currentQuestion

        Do Until Not tempR = currentQuestion
            tempR = random.Next(questions.Count())
        Loop
        currentQuestion = tempR
        lblQuest.Text = questions(currentQuestion)
        randomQuestions += 1

    Else

        Form3.Show()
        Me.Hide()
        MsgBox("Total Right: " & rightQuestions.Count() & vbNewLine & "Total Wrong: " & wrongQuestions.Count())

        Dim wrongQs As String = ""
        For Each wrong As String In wrongQuestions
            If (wrongQs.Count() > 0) Then
                wrongQs &= ", " & questions(wrong)

            Else
                wrongQs &= questions(wrong)

            End If

        Next

        If (wrongQs.Count() > 0) Then MsgBox("Wrong Questions: " & vbNewLine & wrongQs)
    End If


End Sub

Here's the Load Questions and add to list of string code

Private Sub questionGet()
    If (randomQuestions > 0) Then randomQuestions = 0
    Using sr As New StreamReader("Questions.txt")
        Using sr2 As New StreamReader("Answers.txt")

            While sr.Peek <> -1
                While sr2.Peek <> -1
                    Dim line As String = sr.ReadLine()
                    Dim line2 As String = sr2.ReadLine()
                    If (line.Contains("|")) Then
                        Dim splits As String() = line.Split("|")
                        Dim splits1 As String() = line2.Split("|")
                        If (Not gotQA) Then gotQA = True
                        questions.Add(splits(0) & vbNewLine & splits(1) & vbNewLine & splits(2) & vbNewLine & splits(3) & vbNewLine & splits(4) & vbNewLine)
                        answers.Add(splits1(0))
                    End If
                End While
            End While
        End Using
    End Using

End Sub

Upvotes: 1

Views: 155

Answers (1)

Praveen Kodali
Praveen Kodali

Reputation: 61

There are two options:

  1. Delete the current question from the questions list so that you have only the list of unasked questions.
  2. Maintain another array of Boolean values or a dictionary with key as question and value as Boolean to represent whether this question has already been asked.

In the 1st option, since only unasked questions are present, the random will never repeat the question. In the 2nd option, if the random refers to an already asked question, get another random. Repeat until you get an unanswered question.

Upvotes: 1

Related Questions