944,028 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 22475
  • C# RSS
Sep 29th, 2005
0

random number display the same

Expand Post »
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?
C# Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 23
Solved Threads: 16
Posting Whiz in Training
plazmo is offline Offline
206 posts
since Aug 2005
Sep 29th, 2005
0

Re: random number display the same

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:
C# Syntax (Toggle Plain Text)
  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.
Reputation Points: 11
Solved Threads: 0
Newbie Poster
ROGENATOR is offline Offline
17 posts
since Sep 2005
Sep 29th, 2005
0

Re: random number display the same

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



C# Syntax (Toggle Plain Text)
  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. }
Reputation Points: 23
Solved Threads: 16
Posting Whiz in Training
plazmo is offline Offline
206 posts
since Aug 2005
Nov 5th, 2008
0

Re: random number display the same

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
h2c357 is offline Offline
8 posts
since Oct 2006
Nov 5th, 2008
0

Re: random number display the same

way to dredge up a 3 yr old post.
Reputation Points: 196
Solved Threads: 190
Posting Virtuoso
LizR is offline Offline
1,735 posts
since Aug 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
This thread is currently closed and is not accepting any new replies.
Previous Thread in C# Forum Timeline: Project Hangs
Next Thread in C# Forum Timeline: HttpWebRequest on port 8000





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC