We can only generate Random numbers within a specified range. However, this number can be used as the index for an array so that we can select a random element from the array, be it an array of Strings, Images, or whatever. What I mean to say is that the process to selecting random picture is the same as that of selecting random strings.
Another thing that I must mention is that this whole stuff we have seen above is not generating a random string, rather selecting a random string. Generating random strings would generate something like zxjhtsjbdjgasd or asdxcbjdkhasd. You can however, select a dictionary word from a list or an array. But, it wouldn't be generating either.
Back to the first question:-
I am assuming you have a collection of wrong answers and a correct answer for each question. (You don't want to make the correct one obvious by filling the wrong ones with those randomly generated strings we just saw, do you?) You need to shuffle the correct answer's position (1, 2, 3 or 4) each time and you need to select 3 wrong answers from the wrong answer collection.
I'll be using the function created by ViRuaL.
STEP 1: First we generate the random position of the correct answer and store it in our variable:
Dim CorrectCh As Integer = RandomNumber(1, 4)STEP 2: Set the correct answer as the text for the appropriate Radio Button:
Options(CorrectCh - 1).Text = CorrectAnswer(Assuming Options to be a zero based array of Radio Buttons)STEP 3: For each of the remaining positions, generate a random number. Using this number we select a random answer from the list/array of wrong answers and set the text of the Radio Buttons accordingly.
CODE
For I As Integer = 1 To 4
If I = CorrectCh Continue For
Dim Ch As Integer = RandomNumber(0, WrongAnswers.Length - 1)
Dim WrongAnswer As String = WrongAnswers(Ch)
Options(I).Text = WrongAnswer
Next I
Although this code is still not bug free. There's a pretty good chance that the same wrong answer will be repeated. This is because generating a Random Number makes no guarantee that it won't be repeated in the next iteration. To resolve this you can follow either of the ways:-
Method 1Store the selected answers or their IDs in another array. Whenever a new choice is generated, make sure that this number has not been already generated. If it has been then repeat the process again.
CODE
Dim SelectedWrongAnswers As New List(Of Integer)
For I As Integer = 1 To 4
If I = CorrectCh Continue For
Retry:
Dim Ch As Integer = RandomNumber(0, WrongAnswers.Length - 1)
For J as Integer = 0 To SelectedWrongAnswers.Length - 1
If SelectedWrongAnswers(J) = Ch Then
Goto Retry
End If
Next J
SelectedWrongAnswers.Add(Ch)
Dim WrongAnswer As String = WrongAnswers(Ch)
Options(I).Text = WrongAnswer
Next I
The inner loop can be eliminated by using a HashTable as follows:-
CODE
Dim SelectedWrongAnswers As New HashTable
For I As Integer = 1 To 4
If I = CorrectCh Continue For
Retry:
Dim Ch As Integer = RandomNumber(0, WrongAnswers.Length - 1)
If SelectedWrongAnswers(Ch) <> Nothing Then
Goto Retry
End If
Dim WrongAnswer As String = WrongAnswers(Ch)
SelectedWrongAnswers.Add(Ch, WrongAnswer)
Options(I).Text = WrongAnswer
Next I
Method 2This Method is not quite efficient, because the Retry operation may take many iterations if an already generated random number is being generated again. So a better approach is to use a class that generates a non random number. You can find the VB .NET and C# . NET implementation of such a class at
http://maxotek.net/blog/?p=9The following code uses the
RandomNumber class declared at
http://maxotek.net/blog/?p=9CODE
Dim WrongAnswerGenerator As New RandomNunber(0, WrongAnswers.Length - 1)
For I As Integer = 1 To 4
If I = CorrectCh Continue For
Dim Ch As Integer = WrongAnswerGenerator.GetRandomNumber()
Dim WrongAnswer As String = WrongAnswers(Ch)
Options(I).Text = WrongAnswer
Next I
Comment/Reply (w/o sign-up)