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;

      }

   }
}

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.

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

		}

	}
}

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.

way to dredge up a 3 yr old post.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.