Thursday, September 27, 2012

Shuffle array in c#

Following is c# code to shuffle an array:

private List<E> ShuffleMyList<E>(List<E> objInputList)



{

List<E> objRandomList = new List<E>();

Random r = new Random();

int randomIndex = 0;

while (objInputList.Count > 0)



{

randomIndex = r.Next(0, objInputList.Count); //Choose a random object in the list

objRandomList.Add(objInputList[randomIndex]); //add it to the new, random list

objInputList.RemoveAt(randomIndex); //remove to avoid duplicates



}

return objRandomList; //return the new random list



}

No comments:

Post a Comment

How to upload app to macOS

1. Open Terminal Press Cmd (⌘) + Space , type Terminal , and hit Enter . 2. Navigate to Your Build Output Directory Your .app file is likel...