title: C#: Elegant code for getting a random value from an IEnumerable
body: <p>In Python, I can do this:</p>
  
  <pre><code>&gt;&gt;&gt; import random
  &gt;&gt;&gt; ints = [1,2,3]
  &gt;&gt;&gt; random.choice(ints)
  3
  </code></pre>
  
  <p>In C# the first thing I did was:</p>
  
  <pre><code>var randgen = new Random();
  var ints = new int[] { 1, 2, 3 };
  ints[randgen.Next(ints.Length)];
  </code></pre>
  
  <p>But this requires indexing, also the duplication of <code>ints</code> bothers me. So, I came up with this:</p>
  
  <pre><code>var randgen = new Random();
  var ints = new int[] { 1, 2, 3 };
  ints.OrderBy(x=&gt; randgen.Next()).First();
  </code></pre>
  
  <p>Still not very nice and efficient. Is there a more elegant way of getting a random value from an IEnumberable?</p>