0

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. ><

Lamia
  • 49
  • 2
  • 11
  • Since there is confusion about your question, are any of us close to what you want to do? – emartel Nov 21 '12 at 18:57
  • @emartel Yes I think yours is going to be helpful. I got the idea and I will try to adapt it in my application. Thank you. – Lamia Nov 21 '12 at 19:10

2 Answers2

1

use the c# Random() class, and index your array with Next

Random r = new Random();

choice = possibleChoices[r.Next(possibleChoices.Length-1)];

then you can overwrite one of the wrong choices with the correct choice

radioButtons[r.Next(radioButtons.Length-1)] = correctAnswer;

documentation

  • +1... and make sure to read [Random number generator only generating one random number](http://stackoverflow.com/questions/767999/random-number-generator-only-generating-one-random-number) if you have not worked with `Random` before. – Alexei Levenkov Nov 21 '12 at 17:50
  • He wants to randomly assign the right answer to a different radio button, not find a random answer – emartel Nov 21 '12 at 17:50
  • This is new for me but I'll try to understand it. Thank you so much. – Lamia Nov 21 '12 at 19:06
0

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 app

List<RadioButton> buttons = new List<RadioButton>();
buttons.Add(answer);
buttons.Add(answer2);
buttons.Add(answer3);
buttons.Add(answer4);

int goodAnswerPos = random.Next(buttons.Count);
buttons[goodAnswerPos].Text = "Good Answer";
buttons.RemoveAt(goodAnswerPos);

foreach (RadioButton button in buttons)
{
    button.Text = "Randomly Selected Wrong Answer";
}

Storing the control at buttons[goodAnswerPos] will allow you to know if the user selected the right one when he submits the answer.

Community
  • 1
  • 1
emartel
  • 7,712
  • 1
  • 30
  • 58
  • Brilliant, I will work on that according to my application, since I'm using pictures for answers. I didn't think about making list of radio buttons. I'm gonna try it :D Thanks a lot. – Lamia Nov 21 '12 at 19:05