Page 1 of 1

# Randomising 2 numbers in a row is giving same random number?

Posted 27 December 2012 - 04:31 AM

hi all , for my little challenge i have been asked to get two random numbers for x and y so i have wrote in

```int x = r.NextDouble();
int y = s.NextDouble();

```

(i have created new randoms ar beginning called r and s)

but when im debugging the code my numbers are coming out exactly the same? now i only just learnt about random numbers so this is only coding i have been taught and dont understand how i can make the numbers actually random ? creat each random number within there own class?

Is This A Good Question/Topic? 0

## Replies To: Randomising 2 numbers in a row is giving same random number?

### #2 raghav.naganathan Reputation: 412
• Posts: 1,449
• Joined: 14-September 12

## Re: Randomising 2 numbers in a row is giving same random number?

Posted 27 December 2012 - 04:38 AM

There is a class called Random which you should use here.

```Random ran = new Random();
int x = ran.Next(10);//creates a number between 0 and 9
int y = ran.Next(10);
```

The Random class is very important over here.

regards,
Raghav

## Re: Randomising 2 numbers in a row is giving same random number?

Posted 27 December 2012 - 05:01 AM

ahhh i see i assumed because i was making two new random numbers i needed to create two new
```Random ran = new Random();

```

plus i wrote that in very beginning and not next to the others coding but it works now thanks for fast reply

### #4 Robin19 Reputation: 272
• Posts: 552
• Joined: 07-July 10

## Re: Randomising 2 numbers in a row is giving same random number?

Posted 27 December 2012 - 11:42 AM

There is no random number with computers. The Random class creates psuedo-random numbers based on the seed number. If you don't supply a seed number, it uses the current time. So creating two Random objects right after each other will usually result in the same seed number to both. Then both Random objects will calculate the same psuedo-random numbers for you. That is why using only one Random object works. It creates one number, then recalculates a different number for you.

### #5 immeraufdemhund Reputation: 79
• Posts: 495
• Joined: 29-March 10

## Re: Randomising 2 numbers in a row is giving same random number?

Posted 27 December 2012 - 11:08 PM

On the other post that has a similar problem it is crazy how true what Robin19 says. If i threw a dart (generates 2 random numbers) without reusing the same Random it will generate the same number it can give you the same number well over 100 times (hard to count when numbers are flying across the screen). Not to mention how long it takes to create a new random number each time.

Someone mentioned in the other post to only make one instance of Random, and use that through out your program. Well the cycle times alone that I measured attest to that. Not to mention that GetDouble will work correctly.

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