4 Replies - 4151 Views - Last Post: 27 December 2012 - 11:08 PM Rate Topic: -----

#1 bboyzeez  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 143
  • Joined: 16-December 12

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  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • 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
Was This Post Helpful? 1
  • +
  • -

#3 bboyzeez  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 143
  • Joined: 16-December 12

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

#4 Robin19  Icon User is offline

  • D.I.C Addict
  • member icon

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

#5 immeraufdemhund  Icon User is offline

  • D.I.C Regular

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

Page 1 of 1