Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If you're on .NET 3.5, you can use the following IEnumerable coolness (VB.NET, not C#, but the idea should be clear...):</p> <pre><code>Random rnd=new Random(); string[] MyRandomArray = MyArray.OrderBy(x =&gt; rnd.Next()).ToArray(); </code></pre> <p>Edit: OK and here's the corresponding VB.NET code:</p> <pre><code>Dim rnd As New System.Random Dim MyRandomArray = MyArray.OrderBy(Function() rnd.Next) </code></pre> <p>Second edit, in response to remarks that System.Random "isn't threadsafe" and "only suitable for toy apps" due to returning a time-based sequence: as used in my example, Random() is perfectly thread-safe, unless you're allowing the routine in which you randomize the array to be re-entered, in which case you'll need something like <code>lock (MyRandomArray)</code> anyway in order not to corrupt your data, which will protect <code>rnd</code> as well. </p> <p>Also, it should be well-understood that System.Random as a source of entropy isn't very strong. As noted in the <a href="http://msdn.microsoft.com/en-us/library/system.random.aspx" rel="noreferrer">MSDN documentation</a>, you should use something derived from <code>System.Security.Cryptography.RandomNumberGenerator</code> if you're doing anything security-related. For example:</p> <pre><code>using System.Security.Cryptography; </code></pre> <p>...</p> <pre><code>RNGCryptoServiceProvider rnd = new RNGCryptoServiceProvider(); string[] MyRandomArray = MyArray.OrderBy(x =&gt; GetNextInt32(rnd)).ToArray(); </code></pre> <p>...</p> <pre><code>static int GetNextInt32(RNGCryptoServiceProvider rnd) { byte[] randomInt = new byte[4]; rnd.GetBytes(randomInt); return Convert.ToInt32(randomInt[0]); } </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