954,514 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

random number display the same

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?

using System;
using System.Threading;


namespace RandomNumber
{
   class RandNum
   {
      [STAThread]
      static void Main(string[] args)
      {
         Console.WriteLine("Random number pairs from 1-6 - type \"q\" to quit");

         Console.WriteLine("{0}, {1}", GetRandNum(1,6), GetRandNum(1,6));
         string p = "";
         while(p != "q")
         {
            p = Console.ReadLine();
            if(p == "")
            {

               Console.WriteLine("{0}, {1}", GetRandNum(1,6), GetRandNum(1,6));
            }
         

      }
      static int GetRandNum(int iStart, int iEnd)
      {
         

         Random r = new Random();

//         //sleeping helps generate random number
         Thread.Sleep(r.Next(100));

         int i = r.Next(iStart, iEnd * iEnd);
         i = i/iEnd + 1;
         return i;

      }

   }
}
plazmo
Posting Whiz in Training
207 posts since Aug 2005
Reputation Points: 23
Solved Threads: 16
 

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:

DateTime x=DateTime.Now();
float mins=x.Minute;
float secs=x.Second;
float hour=x.Hour;
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.

ROGENATOR
Newbie Poster
17 posts since Sep 2005
Reputation Points: 11
Solved Threads: 0
 

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

using System;
using System.Threading;


namespace RandomNumber
{
	class RandNum
	{
		private static Random rNum = new Random();
		[STAThread]
		static void Main(string[] args)
		{
			int iMin = 1;
			int iMax = 6;
			Console.WriteLine("Random number pairs from 1-6 - type \"q\" to quit");
			string p = "";
			while(p != "q")
			{
				if(p == "")
				{
					Console.WriteLine("{0}, {1}", rNum.Next(iMin, iMax), rNum.Next(iMin, iMax));
				}

				p = Console.ReadLine();
			}

		}

	}
}
plazmo
Posting Whiz in Training
207 posts since Aug 2005
Reputation Points: 23
Solved Threads: 16
 

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.

h2c357
Newbie Poster
8 posts since Oct 2006
Reputation Points: 10
Solved Threads: 0
 

way to dredge up a 3 yr old post.

LizR
Posting Virtuoso
1,791 posts since Aug 2008
Reputation Points: 196
Solved Threads: 190
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You