chillydk147
chillydk147

Reputation: 385

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

        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

---------------------------MY LINQ SOLUTION----------------------

        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();

Upvotes: 0

Views: 210

Answers (1)

Dennis_E
Dennis_E

Reputation: 8894

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. I'm struggling to put this in a linq statement, though.

    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]);
    }

Upvotes: 1

Related Questions