Can't get random numbers

Posted 25 August 2019 - 01:06 AM

I'm still in the process of learning C# and I was writing a program to calculate the hypotenuse in a triangle using the Pythagorean theorem and for lines a and b I wanted to use random numbers, but for some reason, it always gives me the same two random numbers. I gave it a scale from 1 to 100 to choose and it always ends up choosing two same numbers like "4,4" or "29,29" or "71,71".

Why is that? I mean when I order it to make random numbers without putting them into the Pythagorean theorem then it just gives me different random numbers, but the moment I put in the lines for Pythagorean theorem then it switches back to giving the same numbers.

Here's the code:

Random random01 = new Random();

double Side_a = random01.Next(1, 100);
double Side_b = random01.Next(1, 100);

double c = Math.Pow(a, 2) + Math.Pow(b, 2);

Console.WriteLine(Side_a,Side_B)/>/>;
Console.WriteLine("Lenght of the hypotenuse is {0}", c);

Is This A Good Question/Topic? 0

Replies To: Can't get random numbers

#2 andrewsw Reputation: 6818
• Posts: 28,229
• Joined: 12-December 12

Re: Can't get random numbers

Posted 25 August 2019 - 01:59 AM

Where do variables a and b come from? Your variables are named side_a and side_b.

Re: Can't get random numbers

Posted 25 August 2019 - 02:43 AM andrewsw, on 25 August 2019 - 01:59 AM, said:

Where do variables a and b come from? Your variables are named side_a and side_b.

You're right. They were just called a & b but i changed them here on this forum into Side_a because i started getting this symbols />/>

#4 andrewsw Reputation: 6818
• Posts: 28,229
• Joined: 12-December 12

Re: Can't get random numbers

Posted 25 August 2019 - 02:58 AM

Seems a bit odd. I would try declaring and assigning the variables as two separate steps, rather than initializing them using Random.

#5 Skydiver Reputation: 7053
• Posts: 23,976
• Joined: 05-May 12

Re: Can't get random numbers

Posted 25 August 2019 - 06:00 AM Never Saint, on 25 August 2019 - 05:43 AM, said:

You're right. They were just called a & b but i changed them here on this forum into Side_a because i started getting this symbols />/>/>

Is that the only thing that you changed for this forum, or did you also change how you are getting the random numbers?

If you truly have code that looks like:
Random random01 = new Random();

double Side_a = random01.Next(1, 100);
double Side_b = random01.Next(1, 100);

then the probabilities that both Side_a and Side_b having the same value are very low. On the other hand if you had code like:
Random random01 = new Random();
Random random02 = new Random();

double Side_a = random01.Next(1, 100);
double Side_b = random02.Next(1, 100);

the the probabilities that both Side_a and Side_b having the same value are very high. This is because the Random object is a pseudo-random number generator. It is initialized based on the current time on the computer. The current time is measure in ticks which is about 10-16 milliseconds. If two pseudo-random number generators use exactly the same algorithm and are seeded with the same number, then they will produce the same sequence of "random" numbers.

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }