Reputation: 782
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?
Upvotes: 2
Views: 339
Reputation: 12776
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();
}
Upvotes: 3
Reputation: 18152
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);
Upvotes: 2