Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>EDIT: I've only just noticed this is LINQ to SQL, not LINQ to Objects. Use Marc's code to get the database to do this for you. I've left this answer here as a potential point of interest for LINQ to Objects.</p> <p>Strangely enough, you don't actually need to get the count. You do, however, need to fetch every element unless you get the count.</p> <p>What you can do is keep the idea of a "current" value and the current count. When you fetch the next value, take a random number and replace the "current" with "new" with a probability of 1/n where n is the count.</p> <p>So when you read the first value, you <em>always</em> make that the "current" value. When you read the second value, you <em>might</em> make that the current value (probability 1/2). When you read the third value, you <em>might</em> make that the current value (probability 1/3) etc. When you've run out of data, the current value is a random one out of all the ones you read, with uniform probability.</p> <p>To apply that with a condition, just ignore anything which doesn't meet the condition. The easiest way to do that is to only consider the "matching" sequence to start with, by applying a Where clause first.</p> <p>Here's a quick implementation. I <em>think</em> it's okay...</p> <pre><code>public static T RandomElement&lt;T&gt;(this IEnumerable&lt;T&gt; source, Random rng) { T current = default(T); int count = 0; foreach (T element in source) { count++; if (rng.Next(count) == 0) { current = element; } } if (count == 0) { throw new InvalidOperationException("Sequence was empty"); } return current; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload