Hi I'm developing a simple -multiple choices- quiz program using visual studio. (C#)
I want to make the choices with radio buttons, in which each radio button has a - randomly selected answer- and one of them is the correct one.
I filled an array with the all possible answers. And I want to know how can I make the right answer not in the same place every time?
so how can I make the right answer goes for a random place? and the other places selects other random numbers from the array which are not the right one. :D
I know my question is not that clear I don't know how to explain. ><
First, select your right answer, then randomly select an index and assign your right answer to the random radio button.
Fill the other radio buttons with random answers.
Tip: store your radio buttons in an list to facilitate this operation. You begin by filling the list with all your radio buttons, then remove them from the list when they're filled with an answer, this way you don't have to handle "which index did I put the right answer in" or "complicated code that references manually the controls by name"
Edit: As pointed out by Alexei Levenkov in another answer, see this thread for more information on how to generate random numbers properly
Assuming a
Random random
declared in your appStoring the control at
buttons[goodAnswerPos]
will allow you to know if the user selected the right one when he submits the answer.use the c#
Random()
class, and index your array withNext
then you can overwrite one of the wrong choices with the correct choice
documentation