Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 42769
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T15:23:24+00:00 2026-05-10T15:23:24+00:00

What is the best way to randomize an array of strings with .NET? My

  • 0

What is the best way to randomize an array of strings with .NET? My array contains about 500 strings and I’d like to create a new Array with the same strings but in a random order.

Please include a C# example in your answer.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T15:23:24+00:00Added an answer on May 10, 2026 at 3:23 pm

    First, you should use the most upvoted answer to this question so far, which is https://stackoverflow.com/a/110570/1757994.

    You can shuffle using LINQ.

    var myShuffledArray = myArray.Shuffle().

    Add the following code as a file called EnumerableExtensions.cs to your project:

    public static class EnumerableExtensions {          public static IList<T> Shuffle<T>(this IEnumerable<T> sequence)     {         return sequence.Shuffle(new Random());     }      public static IList<T> Shuffle<T>(this IEnumerable<T> sequence, Random randomNumberGenerator)     {         if (sequence == null)         {             throw new ArgumentNullException("sequence");         }          if (randomNumberGenerator == null)         {             throw new ArgumentNullException("randomNumberGenerator");         }          T swapTemp;         List<T> values = sequence.ToList();         int currentlySelecting = values.Count;         while (currentlySelecting > 1)         {             int selectedElement = randomNumberGenerator.Next(currentlySelecting);             --currentlySelecting;             if (currentlySelecting != selectedElement)             {                 swapTemp = values[currentlySelecting];                 values[currentlySelecting] = values[selectedElement];                 values[selectedElement] = swapTemp;             }         }          return values;     } } 

    Adapted from https://github.com/microsoft/driver-utilities/blob/master/src/Sarif.Driver/EnumerableExtensions.cs

    It is a bad idea to use informally authored shuffle algorithms, since they are hard to analyze for flaws.

    If you’re using C# 7.0 or higher, this other approach is slow for many elements but works correctly in all environments C# is used:

    var rnd = new Random(); var myRandomArray = myArray  .Select(x => (x, rnd.Next()))  .OrderBy(tuple => tuple.Item2)  .Select(tuple => tuple.Item1)  .ToArray();     

    This is almost as verbose as the Fisher-Yates shuffle, which is much faster for large numbers of elements. This creates random numbers for each element, then sorts by the random number.

    This answer used to say something along the lines of .OrderBy(x => random()).

    Experienced developers will correctly know that .OrderBy(x => random()) alone is wrong. So if you commit code that contains it, you will look bad.

    Why is it wrong? OrderBy expects the keySelector, the function that it is passed, to be pure. That means the keySelector should return the same answer every time it is called and cause no side effects.

    C# implementations like Unity’s do not cache the result of the selector passed to OrderBy. Games tend to shuffle things. It is hard to generalize across all CLR implementations, so it’s better to just use the correct thing. In order to not look bad with code you author, you should use Fisher-Yates instead.

    For Visual Basic and versions of C# earlier than 7.0, use Fisher-Yates. A correct implementation using only LINQ is more verbose than implementing Fisher-Yates, so you will have written more code to do a worse implementation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 58k
  • Answers 58k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer You don't need a BackgroundWorker unless you want to be… May 11, 2026 at 8:45 am
  • added an answer I hate to say it (or rather the anti-table crowd… May 11, 2026 at 8:45 am
  • added an answer Most common application: it enables an anonymous function to call… May 11, 2026 at 8:44 am

Top Members

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.