2

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;
    }
iam.Carrot
  • 4,976
  • 2
  • 24
  • 71
  • You need to pass a seed to the Random constructor. – KreepN Jul 17 '15 at 13:26
  • 1
    `Random` isn't really `Random` unless its seeded, usually you can use `Random rnd = new Random((int)DateTime.Now.Ticks);` or something to get a unique number between runs. But as your other comments say, you shouldn't be creating a new `Random` class every time you need a new number. – Ron Beyer Jul 17 '15 at 13:28
  • just re-use the same random and call `.Next(0, 10)` on it. No need for so many objects – Jonesopolis Jul 17 '15 at 13:29
  • Ron you are completely incorrect. Please consult the MSDN entry for the default Random constructor. – Jonathon Reinhart Jul 17 '15 at 13:33
  • Many Thanks all It worked perfectly as soon as I used just one random function. – iam.Carrot Jul 17 '15 at 13:39

3 Answers3

1

You should have only one instance of Random, preferably for your entire program.

Random is automatically seeded with a time-based value when you create it using the default constructor:

Initializes a new instance of the Random class, using a time-dependent default seed value.

https://msdn.microsoft.com/en-us/library/h343ddh9(v=vs.110).aspx

The problem is, you are creating those four instances faster than the resolution of this time-based value. So all four instances are being seeded with the same value. And when called with the same parameters, each will return the same value.

Because of race conditions like this, you should keep just one global instance of Random if possible.

Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328
1

Use one instance and call .Next. Your issue is the seed for all of those Random instances is coming out the same (it defaults to a time-based seed).

The implementation is just an algorithm that requires a starting value to begin deriving other "random" values from. It isn't at all random, you can figure out what the value will be knowing the seed and the algorithm used.

Adam Houldsworth
  • 63,413
  • 11
  • 150
  • 187
1

Don't create a new Random instance every time.

Keep a single Random instance and call Next() for every new random number.

See this SO question for more details.

Community
  • 1
  • 1
Niels Filter
  • 4,430
  • 3
  • 28
  • 42