vendredi 17 juillet 2015

Code For Random not working properly

I am creating a random function to give me four random values of A,B,C,D and the sign(+,-). The problem is the code generates all same values and the output I get has only A=B=C=D=x where x is any random value generated by the code. Also I tried to put a breakpoint and debug it, it works just fine no problems but as soon as I remove the break point the same issue occurs. Can anyone please tell me where am I going wrong. The code is as follows:

public void RandomQuestionGenerate()
    {//all the variables(A,B,C,D,sign1,sign2) are globally defined
        Random FirstNo = new Random();
        A = FirstNo.Next(0, 10);
        Random SecondNo = new Random();
        TextA.Text = A.ToString();

        B = SecondNo.Next(0, 10);
        Random ThirdNo = new Random();
        TextB.Text = B.ToString();

        C = ThirdNo.Next(0, 10);
        Random FourthNo = new Random();
        TextC.Text = C.ToString();

        D = FourthNo.Next(0, 10);
        TextD.Text = D.ToString();

        Random FirstSign = new Random();
        int x = FirstSign.Next(0, 2);
        if(x == 0)
        {
            Sign1 = "+";
        }
        else if(x == 1)
        {
            Sign1 = "-"; 
        }
        TextSign1.Text = Sign1;


        Random SecondSign = new Random();
        int y = SecondSign.Next(0, 2);
        if (y == 0)
        {
            Sign2 = "+";
        }
        else if (y == 1)
        {
            Sign2 = "-";
        }
        TextSign2.Text = Sign2;
    }




Aucun commentaire:

Enregistrer un commentaire