C#  random number generator returns same number .
or
reandom number c# returning same number .

Solution:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace msdn
{
    class Program
    {
        private static readonly Random random = new Random();
        private static readonly object syncLock = new object();

        static void Main(string[] args)
        {
                const int numInt = 3;
            int[] Test = new int[numInt];

            for(int i=0; i<numInt; i++)
            {
                Test[i] = randNum(numInt);
            }

            foreach(int num in Test)
                {
                    Console.WriteLine("Test: " + num);
                }


            Console.ReadKey();
        }
        static int randNum(int i)
        {
            
            int x = 0;
            //Random rand = new Random();
            lock (syncLock)
            { // synchronize
                x = random.Next(1, 20);
                return x;
            }

            
        }
    }
}

Comments

Popular posts from this blog