Created
October 12, 2015 22:06
-
-
Save bluejaysql/9f4afbdc32664c2b400b to your computer and use it in GitHub Desktop.
C Sharp Make a smaller randomised list from another list
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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