开发者

C# Random Pair generation

开发者 https://www.devze.com 2022-12-12 21:02 出处:网络
Assume the possible outcome of a die thrown is one of {1,2,3,4,5,6} When 开发者_StackOverflowtwo dice are thrown three times, I want to collect random

Assume the possible outcome of a die thrown is one of {1,2,3,4,5,6}

When 开发者_StackOverflowtwo dice are thrown three times, I want to collect random outcomes from two dice.

My implementation is

var q = from threeTimes in new int[] { 1, 2, 3 }
                    let set1 = new Random().Next(1, 6)
                    let set2 = new Random().Next(1, 6)
                    select new { s1 = set1, s2 = set2 };

            foreach (var v in q)
            {
                Console.WriteLine("Die1 :{0} Die2 :{1}", v.s1, v.s2);
            }

But most of the times I receive the same values for Die1 and Die2.

I mean

Die1: 5   Die2:  5

Die1: 2   Die2:  2

Die1: 2   Die2:  2

What correction do I need to make in order to get random pairs?


This happens because you are creating two Random objects effectively instantaneously. By default, they are seeded using a time based value, most likely Environment.TickCount, though I haven't seen this confirmed.

Constructing one Random before your query will solve this, as will seeding the two objects separately:

var rnd = new Random();
var q = from threeTimes in new int[] { 1, 2, 3 }
        let set1 = rnd.Next(1, 6)
        let set2 = rnd.Next(1, 6)
        select new { s1 = set1, s2 = set2 };


Give your Random a seed (its other constructor) and keep using the same instance instead of newing one up every time.


Create one Random() instance, then call .Next() on that:

Random random = new Random();
var q = from threeTimes in new int[] { 1, 2, 3 }
                    let set1 = random.Next(1, 6)
                    let set2 = random.Next(1, 6)
                    select new { s1 = set1, s2 = set2 };

            foreach (var v in q)
            {
                Console.WriteLine("Dice1 :{0} Dice2 :{1}", v.s1, v.s2);
            }


What happens in you run random.Next(); In between?

I think that two calls in the same second will retrieve the same value when using the constructor, in that way you are using it. Try to create a instance of Random and use that one instead.

Random r = new Random(); let set1 = r.next(1,6); let set2 = r.next(1,6);


Bart de Smet has a pretty heavy duty article on this you might want to check.

0

精彩评论

暂无评论...
验证码 换一张
取 消