-3

I need to fill an array of size 12 with random numbers from 0 to 11 but the numbers need to be unrepeated. For example {10,2,4,8,6,3,1,9,0,7,5,11} Here's the work I tried but it didn't work

Random rnd = new Random(); 
      for (int i = 0; i <= 11; i++) 
      {
          int MoveNumber = rnd.Next(0, 11);
          for (int z = 0; z <= 11; z++) 
          {
              if (usednums[z] != MoveNumber) 
              {
                  usednums[i] = MoveNumber;
              }
          }          
      } 
ɐsɹǝʌ ǝɔıʌ
  • 4,440
  • 3
  • 35
  • 56
  • 1
    In what way didn't it work? – Ant P May 11 '14 at 16:43
  • Hint: you want to *shuffle* the numbers 0-11. Search for "shuffle" and "C#" on Stack Overflow... – Jon Skeet May 11 '14 at 16:44
  • 1
    try to explain the algorithm in plain English words to someone or to yourself, that way, you can understand what you are actually doing. – Can Poyrazoğlu May 11 '14 at 16:44
  • possible duplicate of [Listing all permutations of a string/integer](http://stackoverflow.com/questions/756055/listing-all-permutations-of-a-string-integer) – anydot May 11 '14 at 16:58

1 Answers1

1

The solution is to create an filled array with the numbers and shuffle it:

var random = new Random();
var numbers = Enumerable.Range(0, 12).OrderBy(r => random.Next()).ToArray();
Jann Westermann
  • 291
  • 1
  • 2