random number display the same

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Aug 2005
Posts: 206
Reputation: plazmo is an unknown quantity at this point 
Solved Threads: 16
plazmo's Avatar
plazmo plazmo is offline Offline
Posting Whiz in Training

random number display the same

 
0
  #1
Sep 29th, 2005
im having problems with the following code
it works fine when i have the sleep in
but without the sleep it writes the same number twice
does this have something to do with the way the computer generates the random number? like based on the computers time?
  1.  
  2.  
  3. using System;
  4. using System.Threading;
  5.  
  6.  
  7. namespace RandomNumber
  8. {
  9. class RandNum
  10. {
  11. [STAThread]
  12. static void Main(string[] args)
  13. {
  14. Console.WriteLine("Random number pairs from 1-6 - type \"q\" to quit");
  15.  
  16. Console.WriteLine("{0}, {1}", GetRandNum(1,6), GetRandNum(1,6));
  17. string p = "";
  18. while(p != "q")
  19. {
  20. p = Console.ReadLine();
  21. if(p == "")
  22. {
  23.  
  24. Console.WriteLine("{0}, {1}", GetRandNum(1,6), GetRandNum(1,6));
  25. }
  26.  
  27.  
  28. }
  29. static int GetRandNum(int iStart, int iEnd)
  30. {
  31.  
  32.  
  33. Random r = new Random();
  34.  
  35. // //sleeping helps generate random number
  36. Thread.Sleep(r.Next(100));
  37.  
  38. int i = r.Next(iStart, iEnd * iEnd);
  39. i = i/iEnd + 1;
  40. return i;
  41.  
  42. }
  43.  
  44. }
  45. }
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 17
Reputation: ROGENATOR is an unknown quantity at this point 
Solved Threads: 0
ROGENATOR's Avatar
ROGENATOR ROGENATOR is offline Offline
Newbie Poster

Re: random number display the same

 
0
  #2
Sep 29th, 2005
Greeting:
That a usual problem with random number, the best way to minimize this to use a seed for the random based on the actual time, something like this:
  1. DateTime x=DateTime.Now();
  2. float mins=x.Minute;
  3. float secs=x.Second;
  4. float hour=x.Hour;
  5. Random r=new Random((secs*mins)+hour);

This way unless you run your program at the exact same hour every time, you`ll have different random numbers each time.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 206
Reputation: plazmo is an unknown quantity at this point 
Solved Threads: 16
plazmo's Avatar
plazmo plazmo is offline Offline
Posting Whiz in Training

Re: random number display the same

 
0
  #3
Sep 29th, 2005
i fixed it, it was because i was creating 2 new randoms at the same time which gave them both the same seed.
so i just made outside the method



  1. using System;
  2. using System.Threading;
  3.  
  4.  
  5. namespace RandomNumber
  6. {
  7. class RandNum
  8. {
  9. private static Random rNum = new Random();
  10. [STAThread]
  11. static void Main(string[] args)
  12. {
  13. int iMin = 1;
  14. int iMax = 6;
  15. Console.WriteLine("Random number pairs from 1-6 - type \"q\" to quit");
  16. string p = "";
  17. while(p != "q")
  18. {
  19. if(p == "")
  20. {
  21. Console.WriteLine("{0}, {1}", rNum.Next(iMin, iMax), rNum.Next(iMin, iMax));
  22. }
  23.  
  24. p = Console.ReadLine();
  25. }
  26.  
  27. }
  28.  
  29. }
  30. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 8
Reputation: h2c357 is an unknown quantity at this point 
Solved Threads: 0
h2c357 h2c357 is offline Offline
Newbie Poster

Re: random number display the same

 
0
  #4
Nov 5th, 2008
Standard way to create random numbers is to create a Random object in class wide or global scope and use object's Next() method to get next random number.

This article explains this in detail.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,735
Reputation: LizR has a spectacular aura about LizR has a spectacular aura about 
Solved Threads: 186
LizR LizR is offline Offline
Posting Virtuoso

Re: random number display the same

 
0
  #5
Nov 5th, 2008
way to dredge up a 3 yr old post.
Did I just hear "You gotta help us, Doc. We've tried nothin' and we're all out of ideas" ? Is this you? Dont let this be you! I will put in as much effort as you seem to.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C# Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC