Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Memory Anomaly: C#

Options
  • 07-11-2006 4:51pm
    #1
    Registered Users Posts: 7,468 ✭✭✭


    If I execute this method I get a list of 100 random numbers, which is fine:
    public void createRandomNumberFile(bool Authenticated)
    {
        StreamWriter sw = new StreamWriter(@"C:\stuff\ran2.csv");
        Random ran = new Random();
        int a;
        for (int i = 0; i < 100; i++)
        {
            a = ran.Next(1000000, 9999999);
            sw.WriteLine(a.ToString() + ",");
        }
        sw.Close();
    }
    

    However, If I execute the code below I get the same number repeated 100 times (which is the anomaly). If I step through the code the number is not repeated. As far as I can see they should both produce a list of 100 random numbers. I've seen this before, can somebody explain why it's happening?
    [b]public int MakeRandom()
    {
        Random ran = new Random();
        return ran.Next(1000000, 9999999);
    }[/b]
    
    public void createRandomNumberFile(bool Authenticated)
    {
        StreamWriter sw = new StreamWriter(@"C:\stuff\ran2.csv");
        int a;
        for (int i = 0; i < 100; i++)
        {
            [b]a = MakeRandom();[/b]
            sw.WriteLine(a.ToString() + ",");
        }
        sw.Close();
        
    }
    


Comments

  • Moderators, Society & Culture Moderators Posts: 9,689 Mod ✭✭✭✭stevenmu


    No, I don't think it's a memory problem, I think it's to do with how the psuedo random generator works and the fact that you're not seeding it with anything.
    As you know it can't generate truely random numbers, it just generates a sequence of numbers which would appear to be random. Each Random() object you create is going to use the exact same sequence over and over (unlesss you use something like the current time to seed it).

    In your first code example you use the one Random object and keep calling Next() on it which returns the next number in the sequence. In your second example you keep creating a new Random object for each iteration so each time you call Next() you're actually getting the first number in the sequence, giving you the same number over and over.

    The way around this would be to use something like the current time in milliseconds as the 'seed', I'm pretty sure one of the Random() constructors allows this.


  • Registered Users Posts: 7,468 ✭✭✭Evil Phil


    Thanks Steve.

    /me gets up off ass and reads the help file.

    Yep, the default constructor (Random()) uses a time-dependent default value. That would explain why I get a different numbers with the second code example if I step through it or delay the loop somehow.


  • Closed Accounts Posts: 4,943 ✭✭✭Mutant_Fruit


    The reason for this is that if you declare Random r = new Random(5); you will get the same sequence of random numbers *every* time. This means that you can reproduce results.

    Once your code is out of testing, you can change that to Random r = new Random(Environment.TickCount); which will make a new sequence of random numbers based on how many ticks (100ns?) have passed since the system booted. It's usually the best way to seed the Random constructor.

    EDIT: It's always possibly that the default constuctor internally uses Environment.TickCount to set the seed. The moral of the story: Don't recreate a new Random class in tight loops unless you have a guaranteed way to supply a 'random' seed value.


Advertisement