Question Method only run once during object creation

bondra

Well-known member
Joined
Oct 24, 2020
Messages
77
Programming Experience
Beginner
For some reason which I can't figure out, my static method ShuffleProbability() only runs once during the creation of objects.
I was hoping that for each object creation it would go into the ShuffleProbability method and generate either false/true and set the property IsHungry to the returned value?

C#:
        public void CreateDummyAnimals()
        {
            //Create dummy animals at start

            Petowner.Pets.Add(new Cat
            {
                Name = "Missy",
                IsHungry = ShuffleProbability(),
            });
            Petowner.Pets.Add(new Dog
            {
                Name = "Bruno",
                IsHungry = ShuffleProbability(),
            });          
        }

ShuffleProbability method:
        public static bool ShuffleProbability()
        {
            // Shuffles a bool value with 50% probaility
            Random random = new Random();

            bool randomNumber = random.Next(0, 2) > 0;
            return randomNumber;
        }
 
If you set a breakpoint on ShuffleProbability(), you will actually find that it is called twice.

The problem is that you are instantiating two Random objects in close proximity in time. The two objects end up having the same random seed number so both of them will generate the same sequence of random numbers. You should only create one instance of a Random object and use it for all your cases when you need to generate a random sequence of numbers.

More details can be found in the documentation.

To produce different sequences of random numbers, you can make the seed value time-dependent, thereby producing a different series with each new instance of Random. The parameterized Random(Int32) constructor can take an Int32 value based on the number of ticks in the current time, whereas the parameterless Random() constructor uses the system clock to generate its seed value. However, on the .NET Framework only, because the clock has finite resolution, using the parameterless constructor to create different Random objects in close succession creates random number generators that produce identical sequences of random numbers. The following example illustrates how two Random objects that are instantiated in close succession in a .NET Framework application generate an identical series of random numbers. On most Windows systems, Random objects created within 15 milliseconds of one another are likely to have identical seed values.
 
Thanks! Do you mean that I should instantiate it in every method that will use ShuffleProbability()? Or in the class like below?

C#:
// Did not work
class Petowner
{
    Utility utility = new Utility();
}

// Works
class Petowner
{
    public static void AddAnimals(){
        Utility utility = new Utility();
    }
}
 
More like:
C#:
class WhateverClassContainsTheThingThatNeedsRandom
{
    static Random _random = new Random();

    :

    public static bool RandomTrueOrFalse()
        => _random.Next(0, 2) > 0;

    :
}
 
Last edited:
Oki! So I suppose that means I need to have the method in every class?
What does the => mean by the way?
 
Back
Top Bottom