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?

SethO
  • 2,703
  • 5
  • 28
  • 38
user1005253
  • 404
  • 2
  • 7
  • 21
  • 3
    http://stackoverflow.com/a/768001/284240 pass the random instance to the method since it's called too quickly. – Tim Schmelter Mar 28 '12 at 17:36
  • 6
    You've asked two completely separate questions. They should be in two separate posts - or, ideally, you'll search and find the answers in various duplicate questions for each part. – Jon Skeet Mar 28 '12 at 17:37
  • 4
    C++ and C# are very different languages with very different compile time / runtime behavior. – Brian Rasmussen Mar 28 '12 at 17:37
  • Your only problem is your lack of understanding how the Random class works. It is seeded based on a value. When you intialized it `Random random = new Random();` it generates the seed. Your method approach because of how quickly your code worked ( ns ) had basically the same seed twice. In your later approach you simply request a random number between 1 and 10 twice. Your first approach can work if you simply do it the correct way. – Security Hound Mar 28 '12 at 17:53

1 Answers1

5

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.

Community
  • 1
  • 1
David
  • 72,686
  • 18
  • 132
  • 173