I am developing a webpage in vb.net that will generate a number of multiple choice questions to the user. I need to shuffle the four answers which are already put in an array. Lets assume that I have to following array:
array = {"Correct", "Wrong1", "Wrong2", "Wrong3"}
I tried to use the following method:
Public Shared Function Shuffle(ByVal items() As String) As Array
Dim max_index As Integer = items.Length - 1
Dim rnd As New Random(DateTime.Now.Millisecond)
For i As Integer = 0 To max_index
' Pick an item for position i.
Randomize()
Dim j As Integer = rnd.Next(i, max_index)
' Swap them.
Dim temp As String = items(i)
items(i) = items(j)
items(j) = temp
Next i
Return items
End Function
The function is working really fine, but my problem is that if I have four questions, the answers will be shuffled for each question but the correct answer will be in one position like:
array = {"Wrong1", "Correct", "Wrong2", "Wrong3"}
array = {"Wrong2", "Correct", "Wrong3", "Wrong1"}
array = {"Wrong3", "Correct", "Wrong1", "Wrong2"}
array = {"Wrong1", "Correct", "Wrong3", "Wrong2"}
What I need is the correct answer to change its location from one question to another. Your help is appreciated.
See Question&Answers more detail:os