-5

This WP8 quiz app (questions ommited for obvious reasons) keeps repeating the questions as if it's generating the same random numbers over and over. There's a list creation function that shuffles the questions and the generator itself.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.Windows.Media;

namespace GamingQuiz
{
    public partial class GamePage : PhoneApplicationPage
    {
        public GamePage()
        {
            InitializeComponent();
            ques();
           // PotterGay();

        }
        int MyNumber = 0;
        question[] q = new question[101];
        public void ques()
        {
        //questions are here e.g. q[0]=new question("question","a1","a2","a3","a4",correctanswer)



        }
     // int[] murica = new int[101];
     // int MyNumber = 0;   
        List<int> murica = new List<int>(); 
        public void PotterGay()
        { 
            int hue = 0;
            Random a = new Random(100);

            while (hue<100){
         MyNumber = a.Next(0,100);
            if (!murica.Contains(MyNumber))
            {
                murica.Add(MyNumber);
                hue++; 
            }



            }
        }

        int vCorect = 0;

        public void end()
        {
            intrebare.Visibility = Visibility.Collapsed;
            vd.Text = "DICKY DICKY";
            var1.Visibility = Visibility.Collapsed;
            var2.Visibility = Visibility.Collapsed;
            var3.Visibility = Visibility.Collapsed;
            var4.Visibility = Visibility.Collapsed;

        }

        int j = 0;
        int fatista = 0;
        public void right()
        {
            fatista = murica[j];
          //  Random asdf = new Random();
          ///  j = asdf.Next(100);
            intrebare.Text = q[fatista].quest;
            var1.Content = q[fatista].v1;
            var2.Content = q[fatista].v2;
            var3.Content = q[fatista].v3;
            var4.Content = q[fatista].v4;
            vCorect = q[fatista].gj;
            j++;

        }


        public void start_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            PotterGay();
           right();
            start.Visibility = Visibility.Collapsed;

        }
        int scor = 0;
        int baba = 0;
        private void var_tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            {
                switch ((string)((Button)sender).Name)
                {
                    case "var1":
                        if (vCorect == 1)
                        {
                            scor++;
                            vd.Text = "Correct!";
                            baba++;
                        }
                        else
                        {
                            scor--;
                            vd.Text = "Wrong!";
                            baba++;
                        }
                        break;
                    case "var2":
                        if (vCorect == 2)
                        {
                            scor++;
                            vd.Text = "Correct!";
                            baba++;
                        }
                        else
                        {
                            scor--;
                            vd.Text = "Wrong!";
                            baba++;
                        }
                        break;
                    case "var3":
                        if (vCorect == 3)
                        {
                            scor++;
                            vd.Text = "Correct!";
                            baba++;
                        }
                        else
                        {
                            scor--;
                            vd.Text = "Wrong!";
                            baba++;
                        }
                        break;
                    case "var4":
                        if (vCorect == 4)
                        {
                            scor++;
                            vd.Text = "Correct!";
                            baba++;
                        }
                        else
                        {
                            scor--;
                            vd.Text = "Wrong!";
                            baba++;
                        }
                        break;

                }
            }
            if (baba < 101)
            {
                right();
                puncte.Text = Convert.ToString(scor);
            }
            else
            {

                murica.Clear();
                end();
            }
        }


    }
}
dee-see
  • 23,668
  • 5
  • 58
  • 91
  • 2
    I think this is getting downvotes because you've posted a bunch of code with little explanation and just expect people to read through the whole thing. I also don't see an actual question. Clearly, you would like to solve the problem mentioned in the title, but you don't really phrase a question anywhere in the body. – DCShannon Aug 29 '14 at 17:18

3 Answers3

2

Your problem is here

Random a = new Random(100);

The 100 in there is what is known as the SEED. Randoms which have the same seed will always give the same permutations of numbers.

You'll either want the default

Random a = new Random();

or a seed which is 'random'

Random a = new Random(DateTime.Now.Miliseconds);

To quote the actual documentation

Providing an identical seed value to different Random objects causes each instance to produce identical sequences of random numbers.

If your application requires different random number sequences, invoke this constructor repeatedly with different seed values. One way to produce a unique seed value is to make it time-dependent.

Haedrian
  • 4,240
  • 2
  • 32
  • 53
  • 1
    There is no reason to do the latter over the former; you're doing the same conceptual operation with a tad less entropy than would be there by default. – Servy Aug 29 '14 at 17:16
  • Yeah that's true. I kept it there for completeness' sake to explain the concept of 'seed' – Haedrian Aug 29 '14 at 17:18
1

You should only call new Random once and keep re-using the Random Number Generator. If you keep newing it up with the same seed 100, then the sequence of numbers you get will always be the same.

John Koerner
  • 37,428
  • 8
  • 84
  • 134
0

Firstly, the 100 in the call to generate the RNG is called a seed and by specifying it, you will get a known series of pseudo-random numbers.

Secondly, remove the Random a = new Random(100); line out of the PotterGay() method and make it a class member instead (private Random a = new Random();). You should only need one RNG for this. Fast, repeated calls to the PotterGay() method could wind up generating the same random sequence, even without the specified seed.

Jesse C. Slicer
  • 19,901
  • 3
  • 68
  • 87
  • That's a good point, except I think he's only calling PotterGay once each time the application runs. Unless I'm totally misinterpreting the purpose of start_Tap. – DCShannon Aug 29 '14 at 18:17
  • Tap is the new MouseClick, sufficiently slow and user-random to make the second part of your answer unnecessary. – H H Aug 29 '14 at 18:29