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 7697327
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T21:59:12+00:00 2026-05-31T21:59:12+00:00

I have read somewhere one of the main differences between Java and C++/C# is

  • 0

I have read somewhere one of the main differences between Java and C++/C# is Java does something at Run-time and C# does something at compile-time. Is this true? If so could you explain this a bit more?

In C#, I created a function which takes in two inputs and returns a random number (called RandomNumber(int x, int y)), using the Random Object. I then called this function twice in another function, expecting to get two difference values (two different random numbers). However, I kept getting same values, which baffled me, since I can do the same thing in Java and expect to get different numbers.

I then decided, to remove my function – RandomNumber(int x, int y) and call the Random inside my other function, show below.

    Random random = new Random();
    int randomNum;
    int la;
    randomNum = random.Next(1, 10);
    numOne.Text = randomNum.ToString();
    la = random.Next(1, 10);

This generates two different random numbers. Why is this the case?

  • 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-31T21:59:13+00:00Added an answer on May 31, 2026 at 9:59 pm

    The random number issue has nothing to do with compile-time or run-time. It has everything to do with where the Random class is instantiated.

    The Random class doesn’t really generate true random numbers. They are generated based on a mathematical formula that includes the current date/time and several other bits of data.

    The following:

    Random r = new Random(100)
    for(int i = 0; i < 100; i++)
    {
        Console.WriteLine(r.Netc().ToString());
    }
    

    will generate a series of numbers that looks random.

    This:

    for(int i = 0; i < 100; i++)
    {
        Random r = new Random(100);
        Console.WriteLine(r.Next().ToString());
    }
    

    will produce the same numbers for a few loops, then a new series of similar numbers for a few loops.

    This is because of the formula. In the second example, each Random class is being created with very similar conditions, and will therefore produce the same number until the time changes enough to change the outcome.

    However, with the first example, there is only one Random class and in subsequent iterations of the loop, it knows to produce a different number, because it knows it just generated one in the last loop.

    If your code is calling a function that declared a new Random object and then uses Random.Next then in the scope of your function, Random is a new object on each call. Therefore, if your calls are within a few milliseconds, you’ll get the same result.

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

Sidebar

Related Questions

I have read somewhere that ArrayList's add() and remove() operations run in amortized constant
I have read somewhere about SQL express running as a user instance or something..
I have read somewhere that Silverlight is subset of WPF , then why triggers
I read somewhere the view ids used by JSF framework have a happy side
I think I read somewhere that some modules only have object oriented interfaces (
I have read this post about how to test private methods. I usually do
I read somewhere that when rendering a lot of 3D objects one could merge
I've been battling this one for a while now. I have a stored proc
I read somewhere that one of few weaknesses in data binding model is that
I've read somewhere that one should use a for loop when you are iterating

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.