简体   繁体   English

LINQ-生成一组随机整数,其总和在一个范围内

[英]LINQ - Generate a set of Random Integers whose sum falls inside a range

I have a List "VisitScores" which in this example will need to contain 4 random integer scores with the sum of the 4 scores falling between "min" and "max", in order to generate a single score, I must take 3 random integers from "valueNumbers", so selecting 19,15,21 would give me a score of 55, I know how to generate the random single score but to solve the rest is giving me a headache 我有一个列表“ VisitScores”,在此示例中,该列表将需要包含4个随机整数分数,而这4个分数的总和介于“ min”和“ max”之间,为了生成单个分数,我必须采用3个随机整数从“ valueNumbers”中获取,因此选择19、15、21会给我55分,我知道如何生成随机单项得分,但是解决其余问题却让我头疼

        var visitScores = new List<int>();
        var valueNumbers = new List<int>() { 5, 20, 1, 7, 19, 3, 15, 60, 21, 57, 9 };
        var totalVisits = 4;
        var min = 241;
        var max = 261;

        var r = new Random();
        var randomScore = (from int value1 in valueNumbers
                           from int value2 in valueNumbers
                           from int value3 in valueNumbers
                           select (value1 + value2 + value3)).OrderBy(z => r.NextDouble()).First();

My expected outcome would be visitScores containing a set of 4 random scores, 100 (60,19,21), 44(20,19,5), 73(57,9,7), 40(20,15,5) which is a total of 257 我的预期结果是visitScores,包含一组4个随机分数,分别为100(60,19,21),44(20,19,5),73(57,9,7),40(20,15,5)总共是257

---------------------------MY LINQ SOLUTION---------------------- --------------------------- MY LINQ解决方案-------------------- -

        var randomScores = (from int value1 in valueNumbers
                           from int value2 in valueNumbers
                           from int value3 in valueNumbers
                           select (value1 + value2 + value3)).Distinct().OrderBy(z => r.NextDouble()).ToList();

        var scores = (from int score1 in randomScores
                            from int score2 in randomScores
                            from int score3 in randomScores
                            from int score4 in randomScores
                      where ((score1 + score2 + score3 + score4) > min) && ((score1 + score2 + score3 + score4) < max) &&
                                  new int[] { score1, score2, score3, score4 }
                                  .Distinct().Count() == 4
                      select new List<int>() { score1, score2, score3, score4 }).Distinct().First();

I was thinking to just select 12 random numbers until their sum fall inside the range, then split them up into groups of 3. I think this should work. 我当时想只选择12个随机数,直到它们的总和落入范围之内,然后将它们分成3组。我认为这应该可行。 I'm struggling to put this in a linq statement, though. 不过,我正在努力将其放在linq语句中。

    int totalNumbers = totalVisits * 3;
    int[] selection = new int[totalNumbers];
    int sum;
    do {
        sum = 0;
        for (int i = 0; i < totalNumbers; ++i) {
            sum += selection[i] = valueNumbers[r.Next(valueNumbers.Length)];
        }
    } while (sum < min || sum > max);
    for (int j = 0; j < selection.Length; j+=3) {
        visitScores.Add(selection[j] + selection[j+1] + selection[j+2]);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM