Shuffle Questions and Answers

Pavle

Well-known member
Joined
Oct 4, 2016
Messages
47
Programming Experience
1-3
I am trying to make quiz in C#.I made questions and answers and how should I shuffle them?
public void Questions()
{
string[] questions = new string[4];
label1
.Text = questions[0] = "What variable is used to display whole number";
label1
.Text = questions[1] = "What variable is used to show characters";
label1
.Text = questions[2] = "What variable is used to display words and sentences";
label1
.Text = questions[3] = "What variable is used to display decimal point values";
}
public void Answers()
{
string[] answers = new string[4];
radioButton1
.Text = answers[0] = "int";
radioButton2
.Text = answers[1] = "char";
radioButton3
.Text = answers[2] = "string";
radioButton4
.Text = answers[3] = "float";
}
 
Here is some code that will randomise an array:
private Random rng = new Random();

private void Randomise<T>(ref T[] array)
{
    array = array.OrderBy(e => this.rng.NextDouble()).ToArray();
}
With that code in the same class, you can now do this:
Randomise(ref myArray);
where 'myArray' is any array. The one point to note there is that this code creates a new array and assigns it back to the original variable rather than reordering the original array in-place. That means that any other references to the original array will be unaffected.

By the way, the code you posted could really do with some work.
 
Back
Top Bottom