2

I'm creating a quiz for kids in WPF.

All the questions are coming from a database.

The interface has a textblock for the question and four buttons for the multiple choice answers.

How do I randomly assign the Content of the buttons so that the correct answer isn't in the same button all the time?

Noelle
  • 772
  • 9
  • 18
  • 44
  • possible duplicate of [How to request a random row in SQL?](http://stackoverflow.com/questions/19412/how-to-request-a-random-row-in-sql) – BrokenGlass Apr 12 '12 at 19:27
  • In short, use a random-number generator (such as the .NET Random class) to apply a random order to the set of answers, and, once ordered, assign the answers to the buttons. If you want a less-vague answer, please show us what you've got so far. :) – Dan J Apr 12 '12 at 19:28

2 Answers2

3

You could use a method to shuffle the answers:

  List<string> Shuffle(List<string> answers)
    {
        Random r = new Random();
        Dictionary<int, string> d = new Dictionary<int, string>();
        foreach (var answer in answers)
        {
            d.Add(r.Next(), answer);
        }
        return d.OrderBy(a => a.Key).Select(b => b.Value).ToList();
    }
ionden
  • 12,536
  • 1
  • 45
  • 37
2

Essentially you could do this by just generating a random number between 0 and 3 as your location of your correct answer. Then display the rest of the answers in whatever order they come form the database.

To get the random number you can use:

var placeHolder = new Random().Next(0,3);
Khan
  • 17,904
  • 5
  • 47
  • 59