遗忘海岸

江湖程序员 -Feiph(LM战士)

导航

模拟取球

        static void T8_Wrap()
        {
            ThreadPool.QueueUserWorkItem(o => { T8(); }, null);
            ThreadPool.QueueUserWorkItem(o => { T8(); }, null);
            ThreadPool.QueueUserWorkItem(o => { T8(); }, null);
        }
        static void T8()
        {
           
            var rnd = new Random(Environment.TickCount);
            var total = 1000000000;
          
            var C_W0 = 0;
            var C_W1 = 0;
            var C_W2 = 0;
            var C_W3 = 0;
            for (int i = 0; i < total; i++)
            {
                var whiteCount = 0;
                var list = new List<String>() { "W", "W", "W", "W", "W", "R", "R", "R", "R", "R", "R", "R" };
                for (int step = 0; step < 3; step++)
                {
                    
                
                    var index = rnd.Next(list.Count);
                    var it = list[index];
                    list.Add(it);

                    if (it == "W") whiteCount++;
                }
                if (whiteCount == 0) C_W0++;
                if (whiteCount == 1) C_W1++;
                if (whiteCount == 2) C_W2++;
                if (whiteCount == 3) C_W3++;
            }
            Console.WriteLine("Total:{0},Count:{1},Probability-C_W0:{2}", total, C_W0, (double)C_W0 / (double)total);
            Console.WriteLine("Total:{0},Count:{1},Probability-C_W1:{2}", total, C_W1, (double)C_W1 / (double)total);
            Console.WriteLine("Total:{0},Count:{1},Probability-C_W2:{2}", total, C_W2, (double)C_W2 / (double)total);
            Console.WriteLine("Total:{0},Count:{1},Probability-C_W3:{2}", total, C_W3, (double)C_W3 /(double) total);
            Console.WriteLine("C_W0+C_W1+C_W2+C_W3={0}", C_W3 + C_W2 + C_W1 + C_W0);
        }
View Code

罐里5白7红,随即拿一个,记录颜色,放回,再放一个同颜色的球,

连取三个球,求以下概率。

0白,1白,2白,3白

 

T8_Wrap()在i5处理器上能占用75%的CPU,因为启动了3个线程,而i5有4核。

posted on 2019-04-20 13:43  遗忘海岸  阅读(105)  评论(0编辑  收藏  举报