lundi 30 mars 2020

Does linqpad reuse the random seed between queries

For this code:

void Main()
{
    var testRandom = new TestRandom();
    testRandom.RandGen.Dump("Property1");
    testRandom.RandGen.Dump("Property2");
    TestRandom.rngStatic.Dump("Static field1");
    TestRandom.rngStatic.Dump("Static field2");
    testRandom.rngInstance.Dump("Instance field1");
    testRandom.rngInstance.Dump("Instance field2");
}

// Define other methods, classes and namespaces here

class TestRandom
{
    private static Random _rnd = new System.Random();
    public static int rngStatic = _rnd.Next();
    public int rngInstance = _rnd.Next();
    public int RandGen =>  _rnd.Next();
}

I get the following result in LinqPad:

Property1 167577867

Property2 2076433106

Static field1 1758463813

Static field2 1758463813

Instance field1 1875178546

Instance field2 1875178546

Static Field 1 and 2, Instance Field 1 and 2 show the same random number when run in the same query. This is as expected, however even when I re-run the query, Instance Field 1 and 2 will keep showing the same random number as in previous runs. So I suspect the seed is fixed, but couldn't confirm it.

Second Query run:

Property1 1860313679

Property2 1472007479

Static field1 1758463813

Static field2 1758463813

Instance field1 1370753000

Instance field2 1370753000




Aucun commentaire:

Enregistrer un commentaire