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 8786871
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T21:44:42+00:00 2026-06-13T21:44:42+00:00

I have the following code for Shuffle so far, public static IList<T> Shuffle<T>(this IList<T>

  • 0

I have the following code for Shuffle so far,

    public static IList<T> Shuffle<T>(this IList<T> list)
    {
        var rnd = new Random();
        return list.OrderBy(element => rnd.Next());
    }

and i use it like,

    list = list.Shuffle();

i want to be able to use it like

    list.Shuffle(); // like list.Reverse();

so basically i want to shuffle by reference

I tried the following code,

    public static void Shuffle<T>(this ref IList<T> list)
    {
        var rnd = new Random();
        list.OrderBy(element => rnd.Next());
    }

but it doesn’t work.

Any help is greatly appreciated!

  • 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. Editorial Team
    Editorial Team
    2026-06-13T21:44:43+00:00Added an answer on June 13, 2026 at 9:44 pm

    I wrote this quite some time ago. Probably worth checking that it is true to the linked algorithm. These sorts of thing are notoriously easy to get subtly wrong.

        //Fisher-Yates_shuffle http://en.wikipedia.org/wiki/Fisher-Yates_shuffle
        private static readonly ThreadLocal<Random> RandomThreadLocal =
            new ThreadLocal<Random>(() => new Random());
        public static void Shuffle<T>(this IList<T> list, int seed = -1)
        {
            var r = seed >= 0 ? new Random(seed) : RandomThreadLocal.Value;
            var len = list.Count;
            for (var i = len - 1; i >= 1; --i)
            {
                var j = r.Next(i);
                var tmp = list[i];
                list[i] = list[j];
                list[j] = tmp;
            }
        }
    

    As per comments below, could be overloaded for more flexibility:

        private static readonly ThreadLocal<Random> RandomThreadLocal =
            new ThreadLocal<Random>(() => new Random());
        public static void Shuffle<T>(this IList<T> list, int seed)
        {
            list.Shuffle(new Random(seed));
        }
    
        public static void Shuffle<T>(this IList<T> list)
        {
            list.Shuffle(null);
        }
    
        public static void Shuffle<T>(this IList<T> list, Random rand)
        {
            var r = rand ?? RandomThreadLocal.Value;
    
            var len = list.Count;
            for (var i = len - 1; i >= 1; --i)
            {
                var j = r.Next(i);
                var tmp = list[i];
                list[i] = list[j];
                list[j] = tmp;
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written the following code: public List<Card> DealCards(int numCardsToDeal) { return shuffle.RemoveRange(0,numCardsToDeal-1); }
I have following code function Model(onChanged) { this.array = new Array(); this.onChanged = onChanged;
I have the following code: public static <T extends Comparable<T>> T[] getRandomPermutationOfIntegers(int size) {
I have following code var res = from c in model.Data select new object[]
I have following code for updating user's column public void UpdateLastModifiedDate(string username) { using
I have following code in C# PasswordDeriveBytes DerivedPassword = new PasswordDeriveBytes(Password, SaltValueBytes, HashAlgorithm, PasswordIterations);
I have following code in C# PasswordDeriveBytes DerivedPassword = new PasswordDeriveBytes(Password, SaltValueBytes, HashAlgorithm, PasswordIterations);
I have following code: string date = 13.04.2012; string date2 = (DateTime.Parse(date).AddDays(1)).ToString(); This is
Have following code: public interface ITest { string St1 { get; } } public
I have the following code- <?php $input = array(); for($i=0; $i<15; $i++) $input[]=$i; shuffle($input);

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.