Parallel.For and For yield different results

前端 未结 3 1889
春和景丽
春和景丽 2021-02-13 15:53

If I run this test:

 var r = new Random();
 var ints = new int[13];
 Parallel.For(0, 2000000, i => {            
     var result = r.Next(1, 7) + r.Next(1, 7)         


        
3条回答
  •  借酒劲吻你
    2021-02-13 16:45

    It is thread safety of Random.

    I get the below distribution as expected once I've made the call to Random.Next() thread safe.

    2: 2.76665
    3: 5.5382
    4: 8.30805
    5: 11.13095
    6: 13.8864
    7: 16.6808
    8: 13.8722
    9: 11.14495
    10: 8.3409
    11: 5.5631
    12: 2.76775
    
    public static class Program
    {
        private const int Max = 2000000;
        private static readonly object Lock = new object();
    
        public static void Main()
        {
            var r = new Random();
            var ints = new int[13];
            Parallel.For(0, Max, i =>
            {
                var result = Rand(r, 1, 7) + Rand(r, 1, 7);
                Interlocked.Increment(ref ints[result]);
            });
    
            for (int i = 0; i < ints.Length; i++)
            {
                Console.WriteLine("{0}: {1}",
                    i, ints[i] / ((double)Max) * 100);
            }
        }
    
        private static int Rand(Random random, int minValue, int maxValue)
        {
            lock (Lock)
            {
                return random.Next(minValue, maxValue);
            }
        }
    }
    

提交回复
热议问题