Randomize Questions and Answers
Hi All,
I started looking for some code to randomize questions and answers. I am wondering if someone can tell me why this doesn't work when using 0 to 49 in the ArrayList? The original code was a total of 4 questions. When I use this many, there are no correct answers that load with the question. In "Sub setTheAnswer" if I set 'If totalRemove = 46" instead of 2 then the correct answer loads to button 4 repeatedly.
Code:
Public Class Form1
Dim questions As New ArrayList
Dim answers As New ArrayList
Dim dtQAMain As New DataTable
Dim questionsCopy As New ArrayList
Dim alAnsButton As New ArrayList 'arraylist to store all answer button.
Dim totalScore As Integer
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Method/Function for loading the Q&A
alAnsButton.Add(Button1)
alAnsButton.Add(Button2)
alAnsButton.Add(Button3)
alAnsButton.Add(Button4)
loaddtQA()
loadQsAndAs()
End Sub
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
Me.Close()
End Sub
Private Sub loaddtQA()
dtQAMain = New DataTable
dtQAMain.Columns.Add("Q")
dtQAMain.Columns.Add("A")
For i = 0 To 49
questions.Add("")
answers.Add("")
Next
questions(0) = "Alabama?"
questions(1) = "Alaska?"
questions(2) = "Arizona?"
questions(3) = "Arkansas?"
questions(4) = "California?"
questions(5) = "Colorado?"
questions(6) = "Connecticut?"
questions(7) = "Delaware?"
questions(8) = "Florida?"
questions(9) = "Georgia?"
questions(10) = "Hawaii?"
questions(11) = "Idaho?"
questions(12) = "Illinois?"
questions(13) = "Indiana?"
questions(14) = "Iowa?"
questions(15) = "Kansas?"
questions(16) = "Kentucky?"
questions(17) = "Louisiana?"
questions(18) = "Maine?"
questions(19) = "Maryland?"
questions(20) = "Massachusetts?"
questions(21) = "Michigan?"
questions(22) = "Minnesota?"
questions(23) = "Mississippi?"
questions(24) = "Missouri?"
questions(25) = "Montana?"
questions(26) = "Nebraska?"
questions(27) = "Nevada?"
questions(28) = "New Hampshire?"
questions(29) = "New Jersey?"
questions(30) = "New Mexico?"
questions(31) = "New York?"
questions(32) = "North Carolina?"
questions(33) = "North Dakota?"
questions(34) = "Ohio?"
questions(35) = "Oklahoma?"
questions(36) = "Oregon?"
questions(37) = "Pennsylvania?"
questions(38) = "Rhode Island?"
questions(39) = "South Carolina?"
questions(40) = "South Dakota?"
questions(41) = "Tennessee?"
questions(42) = "Texas?"
questions(43) = "Utah?"
questions(44) = "Vermont?"
questions(45) = "Virginia?"
questions(46) = "Washington?"
questions(47) = "West Virginia?"
questions(48) = "Wisconsin?"
questions(49) = "Wyoming?"
answers(0) = "Montgomery"
answers(1) = "Juneau"
answers(2) = "Phoenix"
answers(3) = "Little Rock"
answers(4) = "Sacramento"
answers(5) = "Denver"
answers(6) = "Hartford"
answers(7) = "Dover"
answers(8) = "Tallahassee"
answers(9) = "Atlanta"
answers(10) = "Honolulu"
answers(11) = "Boise"
answers(12) = "Springfield"
answers(13) = "Indianapolis"
answers(14) = "Des Moines"
answers(15) = "Topeka"
answers(16) = "Frankfort"
answers(17) = "Baton Rouge"
answers(18) = "Augusta"
answers(19) = "Annapolis"
answers(20) = "Boston"
answers(21) = "Lansing"
answers(22) = "Saint Paul"
answers(23) = "Jackson"
answers(24) = "Jefferson City"
answers(25) = "Helena"
answers(26) = "Lincoln"
answers(27) = "Carson City"
answers(28) = "Concord"
answers(29) = "Trenton"
answers(30) = "Santa Fe"
answers(31) = "Albany"
answers(32) = "Raleigh"
answers(33) = "Bismarck"
answers(34) = "Columbus"
answers(35) = "Oklahoma City"
answers(36) = "Salem"
answers(37) = "Harrisburg"
answers(38) = "Providence"
answers(39) = "Columbia"
answers(40) = "Pierre"
answers(41) = "Nashville"
answers(42) = "Austin"
answers(43) = "Salt Lake City"
answers(44) = "Montpelier"
answers(45) = "Richmond"
answers(46) = "Olympia"
answers(47) = "Charleston"
answers(48) = "Madison"
answers(49) = "Cheyenne"
For i = 0 To questions.Count - 1
dtQAMain.Rows.Add(questions(i), answers(i)) 'assign QA in table for scoring purpose later
Next
End Sub
Private Sub loadQsAndAs()
Label1.Visible = False
For i = 0 To alAnsButton.Count - 1
alAnsButton(i).visible = True
Next
questionsCopy = New ArrayList
questionsCopy = questions.Clone 'close a copy so we dont effect the actual question copy when randomize and eliminate asked question from arraylist
TextBox1.Text = setTheQuestion()
setTheAnswer()
End Sub
Private Function setTheQuestion() As String
Dim randomValue As New Random
Dim randomQ As String = ""
Dim index As Integer
If questionsCopy.Count <> 0 Then
index = randomValue.Next(0, questionsCopy.Count - 1)
randomQ = questionsCopy(index)
questionsCopy.RemoveAt(index) 'asked question will be remove.
Else ' questions are finished, show the mark
ShowMark()
End If
Return randomQ
End Function
Private Sub setTheAnswer() 'randonmize the answer and assign to button
If TextBox1.Text = "" Then Exit Sub ' if question finish exit sub
Dim randomValue As New Random
Dim NewIndex As Integer
Dim temp As String
Dim answersCopy As ArrayList = answers.Clone
For n = answersCopy.Count - 1 To 0 Step -1
NewIndex = randomValue.Next(0, n + 1)
' Swap them.
temp = answersCopy(n)
answersCopy(n) = answersCopy(NewIndex)
answersCopy(NewIndex) = temp
Next
Dim AnswerRowCheck As Integer = questions.IndexOf(TextBox1.Text)
Dim ActualAnswer As String = dtQAMain.Rows(AnswerRowCheck).Item("A") 'check actual answer
Dim totalRemove As Integer = 0
For i = answersCopy.Count - 1 To 0 Step -1
If totalRemove = 2 Then Exit For
If answersCopy(i) <> dtQAMain.Rows(AnswerRowCheck).Item("A") Then
answersCopy.RemoveAt(i)
totalRemove += 1
End If
Next 'remove 2 of the selection,since only 4 button for answer selection and should not take out the actual answer
For i = 0 To alAnsButton.Count - 1
alAnsButton(i).text = answersCopy(i)
Next
End Sub
Private Sub ShowMark()
For i = 0 To alAnsButton.Count - 1
alAnsButton(i).text = "" 'clear the text, no more input receive from user.
Next
Label1.Visible = True
Label1.Text = totalScore & " out of 50 are correct."
End Sub
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
totalScore = 0
loadQsAndAs() 'refresh question
End Sub
Private Sub Button4_MouseClick(sender As Object, e As MouseEventArgs) Handles Button4.MouseClick, Button3.MouseClick, Button2.MouseClick, Button1.MouseClick
If sender.text = "" Then Exit Sub
Dim AnswerRowCheck As Integer = questions.IndexOf(TextBox1.Text) 'search question number
If dtQAMain.Rows(AnswerRowCheck).Item("A") = sender.text Then 'checking answer
totalScore += 1
End If
TextBox1.Text = setTheQuestion() 'next question
setTheAnswer()
End Sub
End Class
Re: Randomize Questions and Answers
The number of questions aren't really the problem... the number of answers are. The code creates a copy of the answers, shuffles then removes two items from the list (I'm assuming the original code had as many as 6 answers?) as long as it is not the right answer ... so you start off with 50 answers.... shuffle them, remove the first two (Assuming they're not the right answer) and then display the first 4 ... so... they're always displaying the wrong answers ....
In short, you need to remove more than just 2. you need to keep removing until there's only 4 remaining, inlcuding the correct answer.
-tg
Re: Randomize Questions and Answers
Thanks techgnome... I am still a little foggy on this and how it works. So is the only problem "If totalRemove = 2" ? I have set that number to various different numbers but still get the same result.
Re: Randomize Questions and Answers
Try this…
Code:
questions(0) = "Alabama?"
questions(1) = "Alaska?"
questions(2) = "Arizona?"
questions(3) = "Arkansas?"
questions(4) = "California?"
questions(5) = "Colorado?"
questions(6) = "Connecticut?"
questions(7) = "Delaware?"
questions(8) = "Florida?"
questions(9) = "Georgia?"
questions(10) = "Hawaii?"
questions(11) = "Idaho?"
questions(12) = "Illinois?"
questions(13) = "Indiana?"
questions(14) = "Iowa?"
questions(15) = "Kansas?"
questions(16) = "Kentucky?"
questions(17) = "Louisiana?"
questions(18) = "Maine?"
questions(19) = "Maryland?"
questions(20) = "Massachusetts?"
questions(21) = "Michigan?"
questions(22) = "Minnesota?"
questions(23) = "Mississippi?"
questions(24) = "Missouri?"
questions(25) = "Montana?"
questions(26) = "Nebraska?"
questions(27) = "Nevada?"
questions(28) = "New Hampshire?"
questions(29) = "New Jersey?"
questions(30) = "New Mexico?"
questions(31) = "New York?"
questions(32) = "North Carolina?"
questions(33) = "North Dakota?"
questions(34) = "Ohio?"
questions(35) = "Oklahoma?"
questions(36) = "Oregon?"
questions(37) = "Pennsylvania?"
questions(38) = "Rhode Island?"
questions(39) = "South Carolina?"
questions(40) = "South Dakota?"
questions(41) = "Tennessee?"
questions(42) = "Texas?"
questions(43) = "Utah?"
questions(44) = "Vermont?"
questions(45) = "Virginia?"
questions(46) = "Washington?"
questions(47) = "West Virginia?"
questions(48) = "Wisconsin?"
questions(49) = "Wyoming?"
answers(0) = "Montgomery"
answers(1) = "Juneau"
answers(2) = "Phoenix"
answers(3) = "Little Rock"
answers(4) = "Sacramento"
answers(5) = "Denver"
answers(6) = "Hartford"
answers(7) = "Dover"
answers(8) = "Tallahassee"
answers(9) = "Atlanta"
answers(10) = "Honolulu"
answers(11) = "Boise"
answers(12) = "Springfield"
answers(13) = "Indianapolis"
answers(14) = "Des Moines"
answers(15) = "Topeka"
answers(16) = "Frankfort"
answers(17) = "Baton Rouge"
answers(18) = "Augusta"
answers(19) = "Annapolis"
answers(20) = "Boston"
answers(21) = "Lansing"
answers(22) = "Saint Paul"
answers(23) = "Jackson"
answers(24) = "Jefferson City"
answers(25) = "Helena"
answers(26) = "Lincoln"
answers(27) = "Carson City"
answers(28) = "Concord"
answers(29) = "Trenton"
answers(30) = "Santa Fe"
answers(31) = "Albany"
answers(32) = "Raleigh"
answers(33) = "Bismarck"
answers(34) = "Columbus"
answers(35) = "Oklahoma City"
answers(36) = "Salem"
answers(37) = "Harrisburg"
answers(38) = "Providence"
answers(39) = "Columbia"
answers(40) = "Pierre"
answers(41) = "Nashville"
answers(42) = "Austin"
answers(43) = "Salt Lake City"
answers(44) = "Montpelier"
answers(45) = "Richmond"
answers(46) = "Olympia"
answers(47) = "Charleston"
answers(48) = "Madison"
answers(49) = "Cheyenne"
Dim r as new random
Dim x as Integer = r.Next(0, 50)
Dim q as string = questions(x)
Dim ca as string = answers(x)
Dim i as integer = r.Next(0, 50)
While i = x
i = r.Next(0, 50)
End While
Dim wa1 as string = answers(i)
While i = x or answers(i) = wa1
i = r.Next(0, 50)
End While
Dim wa2 as string = answers(i)
While i = x or answers(i) = wa1 or answers(i) = wa2
i = r.Next(0, 50)
End While
Dim wa3 as string = answers(i)
Re: Randomize Questions and Answers
Quote:
Originally Posted by
mikeg71
Thanks techgnome... I am still a little foggy on this and how it works. So is the only problem "If totalRemove = 2" ? I have set that number to various different numbers but still get the same result.
While the Universal Answer to Life, the Universe, and Everything is 42, in this case you want 46 .... you have 50 possible answers ... you need to leave 4 ... 50 -4 = 46 ... so you need to remove 46 possible answers from the list.
-tg
Re: Randomize Questions and Answers
Quote:
Originally Posted by
.paul.
Try this…
Code:
questions(0) = "Alabama?"
questions(1) = "Alaska?"
questions(2) = "Arizona?"
questions(3) = "Arkansas?"
questions(4) = "California?"
questions(5) = "Colorado?"
questions(6) = "Connecticut?"
questions(7) = "Delaware?"
questions(8) = "Florida?"
questions(9) = "Georgia?"
questions(10) = "Hawaii?"
questions(11) = "Idaho?"
questions(12) = "Illinois?"
questions(13) = "Indiana?"
questions(14) = "Iowa?"
questions(15) = "Kansas?"
questions(16) = "Kentucky?"
questions(17) = "Louisiana?"
questions(18) = "Maine?"
questions(19) = "Maryland?"
questions(20) = "Massachusetts?"
questions(21) = "Michigan?"
questions(22) = "Minnesota?"
questions(23) = "Mississippi?"
questions(24) = "Missouri?"
questions(25) = "Montana?"
questions(26) = "Nebraska?"
questions(27) = "Nevada?"
questions(28) = "New Hampshire?"
questions(29) = "New Jersey?"
questions(30) = "New Mexico?"
questions(31) = "New York?"
questions(32) = "North Carolina?"
questions(33) = "North Dakota?"
questions(34) = "Ohio?"
questions(35) = "Oklahoma?"
questions(36) = "Oregon?"
questions(37) = "Pennsylvania?"
questions(38) = "Rhode Island?"
questions(39) = "South Carolina?"
questions(40) = "South Dakota?"
questions(41) = "Tennessee?"
questions(42) = "Texas?"
questions(43) = "Utah?"
questions(44) = "Vermont?"
questions(45) = "Virginia?"
questions(46) = "Washington?"
questions(47) = "West Virginia?"
questions(48) = "Wisconsin?"
questions(49) = "Wyoming?"
answers(0) = "Montgomery"
answers(1) = "Juneau"
answers(2) = "Phoenix"
answers(3) = "Little Rock"
answers(4) = "Sacramento"
answers(5) = "Denver"
answers(6) = "Hartford"
answers(7) = "Dover"
answers(8) = "Tallahassee"
answers(9) = "Atlanta"
answers(10) = "Honolulu"
answers(11) = "Boise"
answers(12) = "Springfield"
answers(13) = "Indianapolis"
answers(14) = "Des Moines"
answers(15) = "Topeka"
answers(16) = "Frankfort"
answers(17) = "Baton Rouge"
answers(18) = "Augusta"
answers(19) = "Annapolis"
answers(20) = "Boston"
answers(21) = "Lansing"
answers(22) = "Saint Paul"
answers(23) = "Jackson"
answers(24) = "Jefferson City"
answers(25) = "Helena"
answers(26) = "Lincoln"
answers(27) = "Carson City"
answers(28) = "Concord"
answers(29) = "Trenton"
answers(30) = "Santa Fe"
answers(31) = "Albany"
answers(32) = "Raleigh"
answers(33) = "Bismarck"
answers(34) = "Columbus"
answers(35) = "Oklahoma City"
answers(36) = "Salem"
answers(37) = "Harrisburg"
answers(38) = "Providence"
answers(39) = "Columbia"
answers(40) = "Pierre"
answers(41) = "Nashville"
answers(42) = "Austin"
answers(43) = "Salt Lake City"
answers(44) = "Montpelier"
answers(45) = "Richmond"
answers(46) = "Olympia"
answers(47) = "Charleston"
answers(48) = "Madison"
answers(49) = "Cheyenne"
Dim r as new random
Dim x as Integer = r.Next(0, 50)
Dim q as string = questions(x)
Dim ca as string = answers(x)
Dim i as integer = r.Next(0, 50)
While i = x
i = r.Next(0, 50)
End While
Dim wa1 as string = answers(i)
While i = x or answers(i) = wa1
i = r.Next(0, 50)
End While
Dim wa2 as string = answers(i)
While i = x or answers(i) = wa1 or answers(i) = wa2
i = r.Next(0, 50)
End While
Dim wa3 as string = answers(i)
Yeah, that simplifies somethings, but now ca, wa1, wa2, & wa3 need to be further shuffled so that sometimes the correct answer moves around randomly. It may be as simple as putting the 4 possible answers into an array, 0-3, putting ca in index 0 by defualt, then picking a number 0-3 ... and then swapping with that item. In fact, I like your approach better since it's more random than shuffling everything and then removing the first 46 that aren't the right one, and that may more often than not lead to the correct answer being in the first spot.
-tg
Re: Randomize Questions and Answers
thanks paul and techgnome. here is what I have now, but it is not working and still puts the correct answer in the last position. I am not certain what your modified code needs to replace.
Code:
Public Class Form1
Dim questions As New ArrayList
Dim answers As New ArrayList
Dim dtQAMain As New DataTable
Dim questionsCopy As New ArrayList
Dim alAnsButton As New ArrayList 'arraylist to store all answer button.
Dim totalScore As Integer
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Method/Function for loading the Q&A
alAnsButton.Add(Button1)
alAnsButton.Add(Button2)
alAnsButton.Add(Button3)
alAnsButton.Add(Button4)
loaddtQA()
loadQsAndAs()
End Sub
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
Me.Close()
End Sub
Private Sub loaddtQA()
dtQAMain = New DataTable
dtQAMain.Columns.Add("Q")
dtQAMain.Columns.Add("A")
For j = 0 To 49
questions.Add("")
answers.Add("")
Next
questions(0) = "Alabama?"
questions(1) = "Alaska?"
questions(2) = "Arizona?"
questions(3) = "Arkansas?"
questions(4) = "California?"
questions(5) = "Colorado?"
questions(6) = "Connecticut?"
questions(7) = "Delaware?"
questions(8) = "Florida?"
questions(9) = "Georgia?"
questions(10) = "Hawaii?"
questions(11) = "Idaho?"
questions(12) = "Illinois?"
questions(13) = "Indiana?"
questions(14) = "Iowa?"
questions(15) = "Kansas?"
questions(16) = "Kentucky?"
questions(17) = "Louisiana?"
questions(18) = "Maine?"
questions(19) = "Maryland?"
questions(20) = "Massachusetts?"
questions(21) = "Michigan?"
questions(22) = "Minnesota?"
questions(23) = "Mississippi?"
questions(24) = "Missouri?"
questions(25) = "Montana?"
questions(26) = "Nebraska?"
questions(27) = "Nevada?"
questions(28) = "New Hampshire?"
questions(29) = "New Jersey?"
questions(30) = "New Mexico?"
questions(31) = "New York?"
questions(32) = "North Carolina?"
questions(33) = "North Dakota?"
questions(34) = "Ohio?"
questions(35) = "Oklahoma?"
questions(36) = "Oregon?"
questions(37) = "Pennsylvania?"
questions(38) = "Rhode Island?"
questions(39) = "South Carolina?"
questions(40) = "South Dakota?"
questions(41) = "Tennessee?"
questions(42) = "Texas?"
questions(43) = "Utah?"
questions(44) = "Vermont?"
questions(45) = "Virginia?"
questions(46) = "Washington?"
questions(47) = "West Virginia?"
questions(48) = "Wisconsin?"
questions(49) = "Wyoming?"
answers(0) = "Montgomery"
answers(1) = "Juneau"
answers(2) = "Phoenix"
answers(3) = "Little Rock"
answers(4) = "Sacramento"
answers(5) = "Denver"
answers(6) = "Hartford"
answers(7) = "Dover"
answers(8) = "Tallahassee"
answers(9) = "Atlanta"
answers(10) = "Honolulu"
answers(11) = "Boise"
answers(12) = "Springfield"
answers(13) = "Indianapolis"
answers(14) = "Des Moines"
answers(15) = "Topeka"
answers(16) = "Frankfort"
answers(17) = "Baton Rouge"
answers(18) = "Augusta"
answers(19) = "Annapolis"
answers(20) = "Boston"
answers(21) = "Lansing"
answers(22) = "Saint Paul"
answers(23) = "Jackson"
answers(24) = "Jefferson City"
answers(25) = "Helena"
answers(26) = "Lincoln"
answers(27) = "Carson City"
answers(28) = "Concord"
answers(29) = "Trenton"
answers(30) = "Santa Fe"
answers(31) = "Albany"
answers(32) = "Raleigh"
answers(33) = "Bismarck"
answers(34) = "Columbus"
answers(35) = "Oklahoma City"
answers(36) = "Salem"
answers(37) = "Harrisburg"
answers(38) = "Providence"
answers(39) = "Columbia"
answers(40) = "Pierre"
answers(41) = "Nashville"
answers(42) = "Austin"
answers(43) = "Salt Lake City"
answers(44) = "Montpelier"
answers(45) = "Richmond"
answers(46) = "Olympia"
answers(47) = "Charleston"
answers(48) = "Madison"
answers(49) = "Cheyenne"
Dim r As New Random
Dim x As Integer = r.Next(0, 50)
Dim q As String = questions(x)
Dim ca As String = answers(x)
Dim i As Integer = r.Next(0, 50)
While i = x
i = r.Next(0, 50)
End While
Dim wa1 As String = answers(i)
While i = x Or answers(i) = wa1
i = r.Next(0, 50)
End While
Dim wa2 As String = answers(i)
While i = x Or answers(i) = wa1 Or answers(i) = wa2
i = r.Next(0, 50)
End While
Dim wa3 As String = answers(i)
For i = 0 To questions.Count - 1
dtQAMain.Rows.Add(questions(i), answers(i)) 'assign QA in table for scoring purpose later
Next
End Sub
Private Sub loadQsAndAs()
Label1.Visible = False
For i = 0 To alAnsButton.Count - 1
alAnsButton(i).visible = True
Next
questionsCopy = New ArrayList
questionsCopy = questions.Clone 'close a copy so we dont effect the actual question copy when randomize and eliminate asked question from arraylist
TextBox1.Text = setTheQuestion()
setTheAnswer()
End Sub
Private Function setTheQuestion() As String
Dim randomValue As New Random
Dim randomQ As String = ""
Dim index As Integer
If questionsCopy.Count <> 0 Then
index = randomValue.Next(0, questionsCopy.Count - 1)
randomQ = questionsCopy(index)
questionsCopy.RemoveAt(index) 'asked question will be remove.
Else ' questions are finished, show the mark
ShowMark()
End If
Return randomQ
End Function
Private Sub setTheAnswer() 'randonmize the answer and assign to button
If TextBox1.Text = "" Then Exit Sub ' if question finish exit sub
Dim randomValue As New Random
Dim NewIndex As Integer
Dim temp As String
Dim answersCopy As ArrayList = answers.Clone
For n = answersCopy.Count - 1 To 0 Step -1
NewIndex = randomValue.Next(0, n + 1)
' Swap them.
temp = answersCopy(n)
answersCopy(n) = answersCopy(NewIndex)
answersCopy(NewIndex) = temp
Next
Dim AnswerRowCheck As Integer = questions.IndexOf(TextBox1.Text)
Dim ActualAnswer As String = dtQAMain.Rows(AnswerRowCheck).Item("A") 'check actual answer
Dim totalRemove As Integer = 0
For i = answersCopy.Count - 1 To 0 Step -1
If totalRemove = 46 Then Exit For
If answersCopy(i) <> dtQAMain.Rows(AnswerRowCheck).Item("A") Then
answersCopy.RemoveAt(i)
totalRemove += 1
End If
Next 'remove 2 of the selection,since only 4 button for answer selection and should not take out the actual answer
For i = 0 To alAnsButton.Count - 1
alAnsButton(i).text = answersCopy(i)
Next
End Sub
Private Sub ShowMark()
For i = 0 To alAnsButton.Count - 1
alAnsButton(i).text = "" 'clear the text, no more input receive from user.
Next
Label1.Visible = True
Label1.Text = totalScore & " out of 50 are correct."
End Sub
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
totalScore = 0
loadQsAndAs() 'refresh question
End Sub
Private Sub Button4_MouseClick(sender As Object, e As MouseEventArgs) Handles Button4.MouseClick, Button3.MouseClick, Button2.MouseClick, Button1.MouseClick
If sender.text = "" Then Exit Sub
Dim AnswerRowCheck As Integer = questions.IndexOf(TextBox1.Text) 'search question number
If dtQAMain.Rows(AnswerRowCheck).Item("A") = sender.text Then 'checking answer
totalScore += 1
End If
TextBox1.Text = setTheQuestion() 'next question
setTheAnswer()
End Sub
End Class
Re: Randomize Questions and Answers
In my example, q is the random question, ca is the correct answer for that question, and wa1, wa2, wa3 are the 3 random wrong answers…
Re: Randomize Questions and Answers
Code:
Dim shuffled() As String = {ca, wa1, wa2, wa3}
shuffled = shuffled.OrderBy(function(s) r.NextDouble).ToArray
The four answers are now shuffled. You still have the question q and the correct answer ca, and the random 1 right/3 wrong answers are the shuffled array.
Re: Randomize Questions and Answers
As another, alternative example from scratch, I used List(of T). You choose a random item from the list and then remove the item from the list so you won't choose it again.
This paradigm is use multiple times, for the list of questions, for the list of buttons to be filled, and for the list of answers to choose from to fill the buttons.
To reduce the amount of string creation and destruction, e.g. creating a clone of an arraylist of strings, I just put the original answers and questions in a simple arrays of strings.
I then use an index into the arrays to reference which question and answer I'm addressing
A list of Integers (0 to 49) representing the indexes available to the questions array is created.
A random number is picked to index into the list of available indexes, and the selected index identifies the question and answer from the simple arrays.
The index is removed from the list so it can't be selected again, and once all the indexes have been used, then all the questions have been asked.
For the buttons, a list of the buttons is created and a list of the 50 indexes into the answers array. The index of the current question is removed from the list of indexes.
One of the four buttons is randomly selected and the correct answer is placed in that button. The .Tag property is set to "Correct" to indicate this is the correct answer.
The selected button is removed from the list of buttons. The the remaining three buttons are set to answers randomly selected from the list of answer indexes.
When a button is selected, it just checks the tag to see if it is the correct button and process accordingly.
Code:
Public Class Form1
Private questions() As String = {
"Alabama", "Alaska", "Arizona", "Arkansas", "California", "Colorado", "Connecticut", "Delaware", "Florida", "Georgia",
"Hawaii", "Idaho", "Illinois", "Indiana", "Iowa", "Kansas", "Kentucky", "Louisiana", "Maine", "Maryland",
"Massachusetts", "Michigan", "Minnesota", "Mississippi", "Missouri", "Montana", "Nebraska", "Nevada", "New Hampshire", "New Jersey",
"New Mexico", "New York", "North Carolina", "North Dakota", "Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island", "South Carolina",
"South Dakota", "Tennessee", "Texas", "Utah", "Vermont", "Virginia", "Washington", "West Virginia", "Wisconsin", "Wyoming"
}
Private answers() As String = {
"Montgomery", "Juneau", "Phoenix", "Little Rock", "Sacramento", "Denver", "Hartford", "Dover", "Tallahassee", "Atlanta",
"Honolulu", "Boise", "Springfield", "Indianapolis", "Des Moines", "Topeka", "Frankfort", "Baton Rouge", "Augusta", "Annapolis",
"Boston", "Lansing", "Saint Paul", "Jackson", "Jefferson City", "Helena", "Lincoln", "Carson City", "Concord", "Trenton",
"Santa Fe", "Albany", "Raleigh", "Bismarck", "Columbus", "Oklahoma City", "Salem", "Harrisburg", "Providence", "Columbia",
"Pierre", "Nashville", "Austin", "Salt Lake City", "Montpelier", "Richmond", "Olympia", "Charleston", "Madison", "Cheyenne"
}
Private QuestionSet As New List(Of Integer)
Private CorrectCount As Integer
Private rand As New Random
Private CurrentQuestion As Integer
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
InitQuestionSet()
End Sub
Private Sub InitQuestionSet()
QuestionSet.Clear()
QuestionSet = Enumerable.Range(0, 50).ToList
CorrectCount = 0
PickQuestion()
End Sub
Private Sub PickQuestion()
If QuestionSet.Count > 0 Then
Dim answerSet As List(Of Integer) = Enumerable.Range(0, 50).ToList 'all 50 answers are possible for buttons
Dim ButtonList As List(Of Button) = {Button1, Button2, Button3, Button4}.ToList
Dim qIdx = QuestionSet(rand.Next(QuestionSet.Count)) 'Set the question index to one of the remaining indexes
Dim btnIdx = rand.Next(4) 'pick one of the four buttons to hold the correct answer
Label1.Text = questions(qIdx) & "?" 'Show the question
ButtonList(btnIdx).Text = answers(qIdx) 'Set the correct answer in the picked button
ButtonList(btnIdx).Tag = "Correct" 'Note that this button is the correct button
ButtonList.RemoveAt(btnIdx) 'remove correcct button from list of buttons
answerSet.Remove(qIdx) 'remove correct answer from list of possible answers
QuestionSet.Remove(qIdx) 'remove question from question set
CurrentQuestion = qIdx 'save so we can reference later if user chooses wrong answer
'Populate the remaining three buttons with incorrect answers
For Each btn In ButtonList
qIdx = answerSet(rand.Next(answerSet.Count)) 'set the answers index to the one of the remaining answer
btn.Text = answers(qIdx) 'set the button to that (incorrect) answer
btn.Tag = "Incorrect" 'Note that this button is incorrect
answerSet.Remove(qIdx) 'remove index so can't select same wrong answer
Next
Else
MessageBox.Show(String.Format("You got {0} out of 50 correct", CorrectCount))
InitQuestionSet()
End If
End Sub
Private Sub Button_Click(sender As System.Object, e As System.EventArgs) Handles Button4.Click, Button3.Click, Button2.Click, Button1.Click
If DirectCast(sender, Button).Tag.ToString = "Correct" Then
CorrectCount += 1
Else
MessageBox.Show(String.Format("Sorry, correct answer is {0}", answers(CurrentQuestion)))
End If
PickQuestion()
End Sub
End Class
Re: Randomize Questions and Answers
Hi passel... I am just now getting back to this. Simply amazing. It gives me plenty to study on, so thank you so much for placing the comment next to each line. I greatly appreciate the time you spent on this.
paul & techgnome... thank you so much for the help. I am going to play around with all of this code and see how many different variations I can create from this. All great stuff for me :)
Re: Randomize Questions and Answers
I see people initializing huge arrays directly in code. Wouldn't it be much easier to just store all that text in a resource and load it from there?
Re: Randomize Questions and Answers
A beginning generalized approach for asking questions and answers. You'll need a button and rich textbox to try this.
Code:
Private Shared PRNG As New Random
Private Qorder As List(Of Integer)
Private Correct As String
Private Shared Answers As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
Private Sub btnNxtQ_Click(sender As Object, e As EventArgs) Handles btnNxtQ.Click
'need questions?
If Qorder Is Nothing OrElse Qorder.Count = 0 Then
'get Q's in random order
Qorder = Enumerable.Range(0, QA.Elements.Count).OrderBy(Function(r) PRNG.Next).ToList
End If
'get a Q
Dim q As XElement = QA.Elements.Skip(Qorder(0)).Take(1).First
Qorder.RemoveAt(0) 'remove it
'show Q
RichTextBox1.Clear()
RichTextBox1.AppendText(q.<q>.First.Value)
RichTextBox1.AppendText(ControlChars.Cr)
RichTextBox1.AppendText(ControlChars.Cr)
'then show answers
Dim ansIDX As Integer = 0
Correct = ""
For Each a As XElement In q...<a>.OrderBy(Function(r) PRNG.Next) 'note answers in random order
RichTextBox1.AppendText(Answers(ansIDX) & ") ") 'show letter
RichTextBox1.AppendText(a.Value) 'then answer
RichTextBox1.AppendText(ControlChars.Cr)
If a.@c = "y" Then 'correct?
Correct = Answers(ansIDX) 'yes
End If
ansIDX += 1
Next
'for debug
If Correct = "" Then Stop 'should not happen
'RichTextBox1.AppendText(ControlChars.Cr)
'RichTextBox1.AppendText(ControlChars.Cr)
'RichTextBox1.AppendText(Correct)
End Sub
'questions and answers
Private QA As XElement = <questions>
<!-- question answer
contained with an aq element
which has ONE q element that is the question
and as many a elements as desired
the a element that is the correct answer should have a c attribute= y -->.
<aq>
<q>How many states in the US?</q>
<a>41</a>
<a>47</a>
<a>49</a>
<a c="y">50</a>
</aq>
<aq>
<q>How many planets in our solar system?</q>
<a>7</a>
<a c="y">8</a>
<a>9</a>
</aq>
<aq>
<q>How far is it to the moon(in miles)?</q>
<a>9,000</a>
<a>363,104</a>
<a c="y">238,855</a>
<a>123,000</a>
<a>93,000,000</a>
</aq>
<aq>
<a>F</a>
<a c="y">T</a>
<q>Jefferson City is Missouri's capital?</q>
</aq>
<aq>
<a>Little Rock</a>
<a>Trantor</a>
<q>The capital of Alaska is?</q>
<a>Metropolis</a>
<a c="y">Juneau</a>
<a>Gotham City</a>
</aq>
</questions>
States Q's that can be substituted for QA in the above code. Manipulate the answers as needed.
Code:
<questions>
<aq>
<q>The capital of Alabama is?</q>
<a c="y">Montgomery</a>
<a>Harrisburg</a>
<a>Olympia</a>
<a>Charleston</a>
<a>Providence</a>
</aq>
<aq>
<q>The capital of Alaska is?</q>
<a c="y">Juneau</a>
<a>Tallahassee</a>
<a>Augusta</a>
<a>Dover</a>
<a>Salem</a>
</aq>
<aq>
<q>The capital of Arizona is?</q>
<a c="y">Phoenix</a>
<a>Charleston</a>
<a>Denver</a>
<a>Little Rock</a>
<a>Helena</a>
</aq>
<aq>
<q>The capital of Arkansas is?</q>
<a c="y">Little Rock</a>
<a>Lansing</a>
<a>Jefferson City</a>
<a>Dover</a>
<a>Montpelier</a>
</aq>
<aq>
<q>The capital of California is?</q>
<a c="y">Sacramento</a>
<a>Oklahoma City</a>
<a>Lincoln</a>
<a>Dover</a>
<a>Atlanta</a>
</aq>
<aq>
<q>The capital of Colorado is?</q>
<a c="y">Denver</a>
<a>Lansing</a>
<a>Frankfort</a>
<a>Albany</a>
<a>Olympia</a>
</aq>
<aq>
<q>The capital of Connecticut is?</q>
<a c="y">Hartford</a>
<a>Santa Fe</a>
<a>Olympia</a>
<a>Indianapolis</a>
<a>Montpelier</a>
</aq>
<aq>
<q>The capital of Delaware is?</q>
<a c="y">Dover</a>
<a>Madison</a>
<a>Atlanta</a>
<a>Richmond</a>
<a>Indianapolis</a>
</aq>
<aq>
<q>The capital of Florida is?</q>
<a c="y">Tallahassee</a>
<a>Springfield</a>
<a>Honolulu</a>
<a>Augusta</a>
<a>Cheyenne</a>
</aq>
<aq>
<q>The capital of Georgia is?</q>
<a c="y">Atlanta</a>
<a>Baton Rouge</a>
<a>Madison</a>
<a>Denver</a>
<a>Austin</a>
</aq>
<aq>
<q>The capital of Hawaii is?</q>
<a c="y">Honolulu</a>
<a>Frankfort</a>
<a>Columbus</a>
<a>Topeka</a>
<a>Annapolis</a>
</aq>
<aq>
<q>The capital of Idaho is?</q>
<a c="y">Boise</a>
<a>Des Moines</a>
<a>Topeka</a>
<a>Montpelier</a>
<a>Madison</a>
</aq>
<aq>
<q>The capital of Illinois is?</q>
<a c="y">Springfield</a>
<a>Augusta</a>
<a>Boston</a>
<a>Nashville</a>
<a>Lincoln</a>
</aq>
<aq>
<q>The capital of Indiana is?</q>
<a c="y">Indianapolis</a>
<a>Denver</a>
<a>Jefferson City</a>
<a>Sacramento</a>
<a>Montpelier</a>
</aq>
<aq>
<q>The capital of Iowa is?</q>
<a c="y">Des Moines</a>
<a>Baton Rouge</a>
<a>Topeka</a>
<a>Saint Paul</a>
<a>Honolulu</a>
</aq>
<aq>
<q>The capital of Kansas is?</q>
<a c="y">Topeka</a>
<a>Madison</a>
<a>Denver</a>
<a>Olympia</a>
<a>Helena</a>
</aq>
<aq>
<q>The capital of Kentucky is?</q>
<a c="y">Frankfort</a>
<a>Albany</a>
<a>Lincoln</a>
<a>Baton Rouge</a>
<a>Madison</a>
</aq>
<aq>
<q>The capital of Louisiana is?</q>
<a c="y">Baton Rouge</a>
<a>Providence</a>
<a>Bismarck</a>
<a>Augusta</a>
<a>Columbus</a>
</aq>
<aq>
<q>The capital of Maine is?</q>
<a c="y">Augusta</a>
<a>Oklahoma City</a>
<a>Salt Lake City</a>
<a>Frankfort</a>
<a>Honolulu</a>
</aq>
<aq>
<q>The capital of Maryland is?</q>
<a c="y">Annapolis</a>
<a>Dover</a>
<a>Concord</a>
<a>Trenton</a>
<a>Harrisburg</a>
</aq>
<aq>
<q>The capital of Massachusetts is?</q>
<a c="y">Boston</a>
<a>Hartford</a>
<a>Boise</a>
<a>Atlanta</a>
<a>Salt Lake City</a>
</aq>
<aq>
<q>The capital of Michigan is?</q>
<a c="y">Lansing</a>
<a>Juneau</a>
<a>Montgomery</a>
<a>Hartford</a>
<a>Frankfort</a>
</aq>
<aq>
<q>The capital of Minnesota is?</q>
<a c="y">Saint Paul</a>
<a>Indianapolis</a>
<a>Honolulu</a>
<a>Juneau</a>
<a>Salem</a>
</aq>
<aq>
<q>The capital of Mississippi is?</q>
<a c="y">Jackson</a>
<a>Salem</a>
<a>Honolulu</a>
<a>Augusta</a>
<a>Sacramento</a>
</aq>
<aq>
<q>The capital of Missouri is?</q>
<a c="y">Jefferson City</a>
<a>Lincoln</a>
<a>Indianapolis</a>
<a>Juneau</a>
<a>Dover</a>
</aq>
<aq>
<q>The capital of Montana is?</q>
<a c="y">Helena</a>
<a>Augusta</a>
<a>Nashville</a>
<a>Raleigh</a>
<a>Bismarck</a>
</aq>
<aq>
<q>The capital of Nebraska is?</q>
<a c="y">Lincoln</a>
<a>Columbus</a>
<a>Columbia</a>
<a>Raleigh</a>
<a>Tallahassee</a>
</aq>
<aq>
<q>The capital of Nevada is?</q>
<a c="y">Carson City</a>
<a>Montpelier</a>
<a>Pierre</a>
<a>Springfield</a>
<a>Raleigh</a>
</aq>
<aq>
<q>The capital of New Hampshire is?</q>
<a c="y">Concord</a>
<a>Madison</a>
<a>Raleigh</a>
<a>Annapolis</a>
<a>Cheyenne</a>
</aq>
<aq>
<q>The capital of New Jersey is?</q>
<a c="y">Trenton</a>
<a>Jackson</a>
<a>Montpelier</a>
<a>Boston</a>
<a>Honolulu</a>
</aq>
<aq>
<q>The capital of New Mexico is?</q>
<a c="y">Santa Fe</a>
<a>Denver</a>
<a>Springfield</a>
<a>Montgomery</a>
<a>Jackson</a>
</aq>
<aq>
<q>The capital of New York is?</q>
<a c="y">Albany</a>
<a>Oklahoma City</a>
<a>Atlanta</a>
<a>Olympia</a>
<a>Concord</a>
</aq>
<aq>
<q>The capital of North Carolina is?</q>
<a c="y">Raleigh</a>
<a>Saint Paul</a>
<a>Montgomery</a>
<a>Montpelier</a>
<a>Salem</a>
</aq>
<aq>
<q>The capital of North Dakota is?</q>
<a c="y">Bismarck</a>
<a>Columbia</a>
<a>Topeka</a>
<a>Albany</a>
<a>Saint Paul</a>
</aq>
<aq>
<q>The capital of Ohio is?</q>
<a c="y">Columbus</a>
<a>Columbia</a>
<a>Montgomery</a>
<a>Denver</a>
<a>Des Moines</a>
</aq>
<aq>
<q>The capital of Oklahoma is?</q>
<a c="y">Oklahoma City</a>
<a>Nashville</a>
<a>Jefferson City</a>
<a>Raleigh</a>
<a>Helena</a>
</aq>
<aq>
<q>The capital of Oregon is?</q>
<a c="y">Salem</a>
<a>Springfield</a>
<a>Nashville</a>
<a>Baton Rouge</a>
<a>Columbus</a>
</aq>
<aq>
<q>The capital of Pennsylvania is?</q>
<a c="y">Harrisburg</a>
<a>Providence</a>
<a>Raleigh</a>
<a>Lincoln</a>
<a>Bismarck</a>
</aq>
<aq>
<q>The capital of Rhode Island is?</q>
<a c="y">Providence</a>
<a>Dover</a>
<a>Little Rock</a>
<a>Columbus</a>
<a>Topeka</a>
</aq>
<aq>
<q>The capital of South Carolina is?</q>
<a c="y">Columbia</a>
<a>Springfield</a>
<a>Annapolis</a>
<a>Albany</a>
<a>Jackson</a>
</aq>
<aq>
<q>The capital of South Dakota is?</q>
<a c="y">Pierre</a>
<a>Hartford</a>
<a>Augusta</a>
<a>Lincoln</a>
<a>Denver</a>
</aq>
<aq>
<q>The capital of Tennessee is?</q>
<a c="y">Nashville</a>
<a>Annapolis</a>
<a>Cheyenne</a>
<a>Boise</a>
<a>Tallahassee</a>
</aq>
<aq>
<q>The capital of Texas is?</q>
<a c="y">Austin</a>
<a>Phoenix</a>
<a>Nashville</a>
<a>Lansing</a>
<a>Little Rock</a>
</aq>
<aq>
<q>The capital of Utah is?</q>
<a c="y">Salt Lake City</a>
<a>Helena</a>
<a>Raleigh</a>
<a>Carson City</a>
<a>Des Moines</a>
</aq>
<aq>
<q>The capital of Vermont is?</q>
<a c="y">Montpelier</a>
<a>Austin</a>
<a>Frankfort</a>
<a>Little Rock</a>
<a>Dover</a>
</aq>
<aq>
<q>The capital of Virginia is?</q>
<a c="y">Richmond</a>
<a>Helena</a>
<a>Pierre</a>
<a>Santa Fe</a>
<a>Montgomery</a>
</aq>
<aq>
<q>The capital of Washington is?</q>
<a c="y">Olympia</a>
<a>Nashville</a>
<a>Baton Rouge</a>
<a>Des Moines</a>
<a>Helena</a>
</aq>
<aq>
<q>The capital of West Virginia is?</q>
<a c="y">Charleston</a>
<a>Nashville</a>
<a>Juneau</a>
<a>Austin</a>
<a>Des Moines</a>
</aq>
<aq>
<q>The capital of Wisconsin is?</q>
<a c="y">Madison</a>
<a>Charleston</a>
<a>Hartford</a>
<a>Montpelier</a>
<a>Lincoln</a>
</aq>
<aq>
<q>The capital of Wyoming is?</q>
<a c="y">Cheyenne</a>
<a>Raleigh</a>
<a>Salt Lake City</a>
<a>Springfield</a>
<a>Des Moines</a>
</aq>
</questions>
This code, using your arrays, generated the above,
Code:
Dim aq As XElement = <questions></questions>
For idx As Integer = 0 To questions.Length - 1
Dim q As XElement = <aq>
<q>The capital of <%= questions(idx).Trim("?"c) %> is?</q>
<a c="y"><%= answers(idx) %></a>
</aq>
Dim ie As List(Of String)
ie = (From s In answers Where s <> answers(idx) Select s Order By PRNG.Next).ToList
For Each s As String In ie.Take(4)
Dim a As XElement = <a><%= s %></a>
q.Add(a)
Next
aq.Add(q)
Next
Re: Randomize Questions and Answers
Quote:
Originally Posted by
Peter Swinkels
I see people initializing huge arrays directly in code. Wouldn't it be much easier to just store all that text in a resource and load it from there?
Is it much easier?
You write the text in a resource.
You write the text in the Form.
The text from the resource is included in the executable.
The strings from the form are included in the executable.
You write some code to load the array from the resource.
The executable loads the array, no extra code to be written. The possibility exists that this could be done in a fairly efficient way, I don't know.
But the primary reason, for me, in this thread is that this is simply an example. The code was provided as text within the post. No need to zip up and attach a project. If you used a resource, then you would have had to create a project, zip up the sources and post the project and have the user reverse the process.
For a simple example like this, that seems like unnecessary extra work.
Personally, I wouldn't use a resource. I would use a file that the program would read so that I could have any number of quizzes to choose from and could always add more without having to recompile the executable.
Also, for this particular type of quiz, I would probably have included specific State based wrong answers of large or well know cities within the sate which might increase the chance for a wrong selection, e.g. include Los Angeles and San Fransisco for California, and Philadelphia for Pennsylvania or Seattle for Washington, or Hilo for Hawaii or New Orleans for Louisiana, or Saint Louis for Missouri, or Rapid City for South Dakota, etc...
Including only cities that are capitals of other states aids the process of elimination.