0

I am trying to create three threads. Each thread is a player and has three shots. The shots are randomly generated numbers from the thread. I have tried to this by

static int counter = 0;

        static Thread player1 = new Thread(new ThreadStart(Player1Shot));
        static Thread player2 = new Thread(new ThreadStart(Player2Shot));
        static Thread player3 = new Thread(new ThreadStart(Player3Shot));

        static readonly ThreadLocal<Random> random =
            new ThreadLocal<Random>(() => new Random(Interlocked.Increment(ref counter)));

static void Main(string[] args)
        {
            player1.Name = "David";
            player2.Name = "James";
            player3.Name = "Mike";

            player1.Start();
            player2.Start();
            player3.Start();
            //Console.WriteLine("{0}", random.Value.Next());
            Console.ReadLine();
        }

        public static void Player1Shot()
        {
            try
            {
                for (int i = 0; i < 3; i++)
                {
                    Console.WriteLine("{0} shot {1}\n", Thread.CurrentThread.Name, random.Value.Next());
                }

            }

However I want the random numbers to be between 0 and 100? Is this possible?

Cathy Regan
  • 251
  • 1
  • 2
  • 7
  • "Is this possible?" I guess you have tried to run your code or?=! what was the outcome? – Mong Zhu May 12 '17 at 11:22
  • @MongZhu, [`Random.Next()`](https://msdn.microsoft.com/en-us/library/9b3ta19y(v=vs.110).aspx) may return bigger value (up to [int.MaxValue](https://msdn.microsoft.com/en-us/library/system.int32.maxvalue(v=vs.110).aspx)). – Sinatr May 12 '17 at 11:23
  • Yes it generates a 6 digit numbers. I would like only numbers between 0 and 100 – Cathy Regan May 12 '17 at 11:24
  • use random.Next(int minValue, int maxValue) see: https://msdn.microsoft.com/it-it/library/2dx6wyd4(v=vs.110).aspx – Simone Cifani May 12 '17 at 11:35

1 Answers1

0

Not sure what your requirements are...consider something more like:

class Program
{

    static int counter = 0;

    static readonly ThreadLocal<Random> random =
        new ThreadLocal<Random>(() => new Random(Interlocked.Increment(ref counter)));

    static List<Thread> players = new List<Thread>();

    static void Main(string[] args)
    {
        string[] playerNames = {"David", "James", "Mike" };
        foreach(string name in playerNames)
        {
            Thread T = new Thread(new ThreadStart(PlayerShot));
            T.Name = name;
            players.Add(T);
        }

        Console.WriteLine("Waiting for Players to Shoot...\n");
        foreach (Thread player in players)
        {
            player.Start();
        }

        // wait for all players to be done shooting
        foreach (Thread player in players)
        {
            player.Join();
        }

        Console.Write("\nPress [Enter] to quit.");
        Console.ReadLine();
    }

    public static void PlayerShot()
    {
        try
        {
            for (int i = 0; i < 3; i++)
            {
                System.Threading.Thread.Sleep(TimeSpan.FromSeconds(random.Value.Next(3, 11))); // 3 to 10 (INCLUSIVE) Second Delay between shots
                Console.WriteLine("{0} shot {1}\n", Thread.CurrentThread.Name, random.Value.Next(0, 101)); // 0 to 100 (INCLUSIVE)
            }
        }
        catch (Exception ex) { }
    }

}
Idle_Mind
  • 38,363
  • 3
  • 29
  • 40