-1

Hello guys I'm creating a program called Quiz game. Now my problem is how can I generate a non repeating questions? All of my questions is stored in a switch statement. I've tried and searched all of the possible solutions but still I get the same output. My code below is just a sample i didnt paste it all bcoz its too long. Whenever I call the method Question() it will random but sometimes the question that is already ask is being ask again.

    public void Question()
    {

     var random = new Random((int)DateTime.Now.Ticks);
        var randomValue = random.Next(1, 8);
        switch (randomValue)
        {
            case 1:
     ans = 1;

                    btnA.Visible = true;
                    btnB.Visible = true;
                    btnC.Visible = true;
                    btnD.Visible = true;
                    btn50.Enabled = true;
                    btndoubledip.Enabled = true;

                    lblQuestion.Text = "1+1=?";
                    voice = new SpeechSynthesizer();
                    voice.SelectVoiceByHints(VoiceGender.Male, VoiceAge.Child);
                    voice.SpeakAsync(lblQuestion.Text.ToString());

                    btnA.Text = "2";
                    btnB.Text = "1";
                    btnC.Text = "4";
                    btnD.Text = "5";


                }
                break;
Federico Dipuma
  • 17,655
  • 4
  • 39
  • 56
  • 1
    I dont even think your posted code will compile. – maccettura Sep 15 '17 at 16:33
  • 6
    You're really thinking about this in the wrong way. First don't use a switch statement - keep your questions in a `List`, order the list randomly (plenty of ways to do that on Stack Overflow) and then just go through them in the new order. – DavidG Sep 15 '17 at 16:34
  • You're going to need to somehow store the list of questions already asked. Or store a list of questions and remove items from it before choosing another one. – Yuriy Faktorovich Sep 15 '17 at 16:34
  • @maccettura its just a sample dude I just paste my method called Question() – Steve Austin Sep 15 '17 at 16:35
  • @DavidG but is it okay to put that in a method like my public void Question()? – Steve Austin Sep 15 '17 at 16:36
  • To give some intuition for what @DavidG said: if you're playing a card game, you don't randomly generate cards and make sure you haven't already drawn them; instead, you take a deck of cards and randomly shuffle it. Imagine your questions as if it were a game with the questions printed on cards that you draw from a randomized deck and that should help you visualize how to model the game in terms of objects. – Dan Bryant Sep 15 '17 at 16:38
  • You may be falling prey to the [Gambler's Fallacy](https://en.wikipedia.org/wiki/Gambler%27s_fallacy) here. If you generate random numbers from 1 to 7, there's a 1 in 7 chance that the next random number will be the same as the previous one. – EJoshuaS - Stand with Ukraine Sep 15 '17 at 16:38

1 Answers1

1

Use Randomize a List<T> to shuffle the list. So:

private static Random rng = new Random(); 
public static void Shuffle<T>(this IList<T> list) 
{ 
    int n = list.Count; 
    while (n > 1)
    {
          n--;
          int k = rng.Next(n + 1);
          T value = list[k]; 
          list[k] = list[n]; 
          list[n] = value; 
     } 
}

Then just shuffle your list of questions and display them from beginning to end.

List<Question> questions = new List<Question>()
{
    question1, 
    question2, 
    question3
    //... 
}
questions.Shuffle();
foreach (Question question in questions) question.Ask();
Yair Halberstadt
  • 5,733
  • 28
  • 60