如何将线程设置为随机生成的数字

时间:2017-05-12 11:09:23

标签: c# multithreading

我正在尝试创建三个线程。每个线程都是一个玩家,有三个镜头。镜头是线程中随机生成的数字。

我试过这个
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());
                }

            }

但是我希望随机数在0到100之间?这可能吗?

0 个答案:

没有答案