Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7539175
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T07:18:48+00:00 2026-05-30T07:18:48+00:00

Why does this code produce the same output for both method calls? I would

  • 0

Why does this code produce the same output for both method calls? I would have assumed that because one method is a normal public method and is called from an instance then it would generate a different random number to the static method call as the instance is seperate from the one created for the static method call?

class ClassWithStaticMembers
{

    public static int ReturnAnIntStatic()
    {
        Random random = new Random();
        return random.Next();
    }

    public int ReturnAnInt()
    {
        Random random = new Random();
        return random.Next();
    }
}

class Program
{

    static void Main(string[] args)
    {

        ClassWithStaticMembers classWithStaticMembers = new ClassWithStaticMembers();

        //We can do this because the method is declared static. 
        Console.WriteLine(ClassWithStaticMembers.ReturnAnIntStatic());

        //This can be used as we have not declared this method static
        Console.WriteLine(classWithStaticMembers.ReturnAnInt());

        Console.ReadKey();

}

}

Output is as follows:

12055544
12055544

Can someone explain why using a method call from an instance of a class procuces the same result as a method call from a static method? Are the instances generated for the method calls not different?

EDIT: Further to this. Is the instance of ClassWithStaticMembers used to call the public method seperate to the static call. By which I mean would the compiler use the same instance again if it recognises I am making a call to the same class later in the file?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T07:18:50+00:00Added an answer on May 30, 2026 at 7:18 am

    That’s because the Random is by default seeded by the current ticks and since both methods are called at almost the same time, they will produce the same numbers. This is explained in the documentation:

    The default seed value is derived from the system clock and has finite
    resolution. As a result, different Random objects that are created in
    close succession by a call to the default constructor will have
    identical default seed values and, therefore, will produce identical
    sets of random numbers. This problem can be avoided by using a single
    Random object to generate all random numbers. You can also work around
    it by modifying the seed value returned by the system clock and then
    explicitly providing this new seed value to the Random(Int32)
    constructor. For more information, see the Random(Int32) constructor.

    Put a sleep between the 2 method calls to observe the difference:

    Console.WriteLine(ClassWithStaticMembers.ReturnAnIntStatic());
    Thread.Sleep(2000);
    Console.WriteLine(classWithStaticMembers.ReturnAnInt());
    

    Or use a different constructor for the Random class to seed them differently. Or simply use the same static instance of a Random class:

    class ClassWithStaticMembers
    {
        private static Random random = new Random();
    
        public static int ReturnAnIntStatic()
        {
            return random.Next();
        }
    
        public int ReturnAnInt()
        {
            return random.Next();
        }
    }
    
    class Program
    {
    
        static void Main()
        {
            var classWithStaticMembers = new ClassWithStaticMembers();
            Console.WriteLine(ClassWithStaticMembers.ReturnAnIntStatic());
            Console.WriteLine(classWithStaticMembers.ReturnAnInt());
            Console.ReadKey();
        }
    }
    

    That’s the reason why you should never use the Random class when you require real randomness and not pseudo random numbers. For example in cryptography you should use the RNGCryptoServiceProvider instead of the Random class. Once you know the initial seed value that was used to instantiate the Random class, you can predict all the numbers this class is going to generate.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

how does one use code to do this: produce 15 random numbers [EDIT: from
The following Common Lisp code does not produce the output I would expect it
Why does the following code Graphics[Raster[{{Hue[1], Hue[1/3]}, {Hue[2/3], Hue[1/6]}}]] not produce any output? In
Why does this code produce this graph? digraph { rankdir = TB; 1 ->
I'm struggling to produce the same behavior in web service code that uses Deferred
Why does this code work (I see the output 1 2 3): for i
Does this code cause a memory leak: int main(){ int * a = new
Why does this code fail to display the category name Apples using the current
Why does this code: class A { public: explicit A(int x) {} }; class
Why does this code int (*g)(int); int (*h)(char); h = g; In C, give

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.