# Can't get random numbers

Page 1 of 1

## 4 Replies - 388 Views - Last Post: 25 August 2019 - 06:00 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=417152&amp;s=5b86a1374f177f20fc28250772a87da3&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Never Saint

Reputation: 0
• 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);

```

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.

### #3 Never Saint

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

## 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

• Code herder

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.