Skip to content

Instantly share code, notes, and snippets.

@bluejaysql
Created October 12, 2015 22:06
Show Gist options
  • Save bluejaysql/9f4afbdc32664c2b400b to your computer and use it in GitHub Desktop.
Save bluejaysql/9f4afbdc32664c2b400b to your computer and use it in GitHub Desktop.
C Sharp Make a smaller randomised list from another list
private List<int> yourlargelist;
private List<int> yoursmalllist;
private int numberofelementsforsmalllist;
List<int> yourtemporarylist = new List<int>(yourlargelist);
Random rand = new Random();
int n = yourtemporarylist.Count();
while (n > 1)
{
n--;
int k = rand.Next(n + 1);
int value = yourtemporarylist[k];
yourtemporarylist[k] = yourtemporarylist[n];
yourtemporarylist[n] = value;
}
yoursmalllist = yourtemporarylist.Take(numberofelementsforsmalllist).ToList();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment