score:2
Accepted answer
since the original list can be modified, here is a very simple and efficient solution, based on this answer:
public static ienumerable<t> shuffle<t>(this ilist<t> list, random rng)
{
for(int i = list.count - 1; i >= 0; i--)
{
int swapindex = rng.next(i + 1);
yield return list[swapindex];
list[swapindex] = list[i];
}
}
Source: stackoverflow.com
Related Query
- How to implement lazy shuffling of Lists in C#?
- How to implement constrained shuffling of a sequence
- How to merge two lists while adding metadata indicating value source with LINQ statement?
- How to Lazy Load child object with string primary key in Entity Framework Code First?
- How to merge a list of lists with same type of items to a single list of items?
- How do I find the text within a div in the source of a web page using C#
- How are people unit testing code that uses Linq to SQL
- What does this C# code with an "arrow" mean and how is it called?
- How to resolve Value cannot be null. Parameter name: source in linq?
- How do I take the Cartesian join of two lists in c#?
- How to combine more than two generic lists in C# Zip?
- How to implement left join in JOIN Extension method
- How to combine 2 lists using LINQ?
- How to count the number of code lines in a C# solution, without comments and empty lines, and other redundant stuff, etc?
- How to simultaneously sort 2 lists using LINQ?
- How can I implement NotOfType<T> in LINQ that has a nice calling syntax?
- How to join two Lists based on common property
- How to combine two types of C# lists into one?
- How to implement Unit of Work that works with EF and NHibernate
- How would you implement LINQ methods with SelectMany?
- How do i get the difference in two lists in C#?
- How do I implement a dynamic 'where' clause in LINQ?
- How do I get the sum of the Counts of nested Lists in a Dictionary without using foreach?
- How to merge two lists using LINQ?
- How to join unknown number of lists in LINQ
- How to implement SkipWhile with Linq to Sql without first loading the whole list into memory?
- How to implement Linq OrderBy method?
- How merge two lists of different objects?
- how do I treat null lists like empty lists in linq?
- How to merge two lists and remove duplicates
More Query from same tag
- Unable to cast object of type 'System.Byte' to type 'System.String'
- Saving all Weapon ID Numbers with loop
- Nested Generic Lambdas in LINQ
- LINQ Join On Multiple Columns With Different Data Types
- LINQ query not grouping properly
- How to correct processing exceptions in linqXml?
- Date field comparison in linq DataService Query
- Reading the next line using LINQ and File.ReadAllLines()
- Retrieve comma-separated values from IQueryable and add to list
- More Elegant LINQ Alternative to Foreach Extension
- LINQ Modelling Automagically Retrieving Data Via EntityRef
- Group by Distinct using Linq
- LINQ. Reducing the code by using dynamic queries
- LINQ to DataTable Results Filtering
- filtering a linq query, based on the object created from each element in the linq query
- c# LINQ sort a list by a subproperty of a property that is a list of objects
- When does Take get called in a linq query?
- Forming SQL/LINQ query - Ordered elements across different rows sharing logical grouping
- c# iterate list to merge item with same field
- Translating SQL results to LINq with join of literal
- EF6 - using Contains with a list
- Group by clause limiting items per group
- Excel Import : Data not being read if column has different data formats in MVC C# using linq
- Linq to XML not getting all nodes
- Getting COUNT and SKIP TAKE in one operation with Linq to Entities
- Game/Quote of the day - how to?
- Entity & LINQ Method Chain Query
- Apply multiple conditions and null value check in Linq query
- Function in linq select
- In C#, What is the fastest way to search for elements in a list but do a "StartsWith()" search?