title: Random playlist algorithm
body: <p>I need to create a list of numbers from a range (for example from x to y) in a random order so that every order has an equal chance.</p>
  
  <p>I need this for a music player I write in C#, to create play lists in a random order.</p>
  
  <p>Any ideas?</p>
  
  <p>Thanks.</p>
  
  <p><strong>EDIT:</strong> I'm not interested in changing the original list, just pick up random indexes from a range in a random order so that every order has an equal chance. </p>
  
  <p>Here's what I've wrriten so far:</p>
  
  <pre><code>    public static IEnumerable&lt;int&gt; RandomIndexes(int count)
      {
          if (count &gt; 0)
          {
              int[] indexes = new int[count];
              int indexesCountMinus1 = count - 1;
  
              for (int i = 0; i &lt; count; i++)
              {
                  indexes[i] = i;
              }
  
              Random random = new Random();
  
              while (indexesCountMinus1 &gt; 0)
              {
                  int currIndex = random.Next(0, indexesCountMinus1 + 1);
                  yield return indexes[currIndex];
  
                  indexes[currIndex] = indexes[indexesCountMinus1];
                  indexesCountMinus1--;
              }
  
              yield return indexes[0];
          }
      }
  </code></pre>
  
  <p>It's working, but the only problem of this is that I need to allocate an array in the memory in the size of <code>count</code>. I'm looking for something that dose not require memory allocation.</p>
  
  <p>Thanks.</p>