5

I want to generate an array that has 144 number from 1->36 in random order (so each number is repeated 4 times). Can we use Enumerable.Repeat and Enumerable.Range to do that. If yes than please explain to me how?

tshepang
  • 12,111
  • 21
  • 91
  • 136
A New Chicken
  • 703
  • 1
  • 8
  • 18

4 Answers4

10

Well, creating the sequence with all the numbers in is easy:

var items = from x in Enumerable.Range(1, 36)
            from y in Enumerable.Repeat(x, 4)
            select y;

Then you can just use ToArray to get it into an array and shuffle it. There are numerous questions about shuffling an array in C# on SO, such as this one. You could either use that code directly, or call ToArray and shuffle the array in place without yielding it at the end.

Community
  • 1
  • 1
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
0
int[] numbers = Enumerable.Range(0, 144).Select(i => (i % 36)+1).OrderBy(g => Guid.NewGuid()).ToArray();
David Hedlund
  • 128,221
  • 31
  • 203
  • 222
  • Probably not important in this case, but this way of shuffling ends up being O(n log n) where it only needs to be O(n). – Jon Skeet Jan 07 '10 at 12:33
0
// Generate the list (not in random order)
var one_to_36 = Enumerable.Range(1, 36);
var lst = one_to_36.Concat(one_to_36).Concat(one_to_36).Concat(one_to_36).ToList();

// Randomize the list by swapping random elements
Random rnd = new Random();
for(int i = 0; i < lst.Count; i++)
{
    int i1 = rnd.Next(lst.Count);
    int i2 = rnd.Next(lst.Count);
    int tmp = lst[i1];
    lst[i1] = lst[i2];
    lst[i2] = tmp;
}
Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758
  • Swapping random elements in this way doesn't give a good random distribution - there's a better way of shuffling a collection, as linked to in my answer. – Jon Skeet Jan 07 '10 at 12:32
0
var seq = Enumerable.Range(0, 144);    
var all = seq.ToList();
var random = new Random();
var result = seq.Select(i => {
 var index = random.Next()%all.Count;
 var r = all[index] % 36 + 1; all.RemoveAt(index);
 return r;
}).ToList();
George Polevoy
  • 7,450
  • 3
  • 36
  • 61