4 Replies - 388 Views - Last Post: 25 August 2019 - 06:00 AM Rate Topic: -----

#1 Never Saint   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 25-August 19

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);

Console.ReadLine();



Is This A Good Question/Topic? 0
  • +

Replies To: Can't get random numbers

#2 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6818
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#3 Never Saint   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 25-August 19

Re: Can't get random numbers

Posted 25 August 2019 - 02:43 AM

View Postandrewsw, 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 B)/>/>
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6818
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#5 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 7053
  • View blog
  • Posts: 23,976
  • Joined: 05-May 12

Re: Can't get random numbers

Posted 25 August 2019 - 06:00 AM

View PostNever 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 B)/>/>/>

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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1