# How can i shuffle a list

Using

``````using System.Collections.Generic;
``````

``````private List<string> alpha = new List<string>{"A", "B", "C", "D"};
``````

I need a new shuffled list

eg :- {“C”, “B”, “D”, “A”};

Using for loop i will show values in list.

How can i shuffle a list?

for (int i = 0; i < alpha.Count; i++) {
string temp = alpha*;*

• ``````  	int randomIndex = Random.Range(i, alpha.Count);*
``````

_ alpha = alpha[randomIndex];_
* alpha[randomIndex] = temp;*
* }*

Try that :
`listRand.Sort((a, b)=> 1 - 2 * Random.Range(0, 1));`

//================================================================//
//===================Fisher_Yates_CardDeck_Shuffle====================//
//================================================================//

``````	/// With the Fisher-Yates shuffle, first implemented on computers by Durstenfeld in 1964,
///   we randomly sort elements. This is an accurate, effective shuffling method for all array types.

public static List<GameObject> Fisher_Yates_CardDeck_Shuffle (List<GameObject>aList) {

System.Random _random = new System.Random ();

GameObject myGO;

int n = aList.Count;
for (int i = 0; i < n; i++)
{
// NextDouble returns a random number between 0 and 1.
// ... It is equivalent to Math.random() in Java.
int r = i + (int)(_random.NextDouble() * (n - i));
myGO = aList[r];
aList[r] = aList*;*
``````

_ aList = myGO;_
* }*

* return aList;*
* }*

I searched for an answer for this myself, and got so aggravated with all the different answers, that I made a clear concise way to do this myself:

``````public List<float> toShuffle = new List<float>();
public List<float> shuffled = new List<float>();

// ShuffleList(toShuffle);

public void ShuffleList(List<float> list)
{
List<float> temp = new List<float>();

for (int i = 0; i < list.Count; i++)
{
int index = Random.Range(0, temp.Count - 1);
temp.RemoveAt(index);
}
}
``````

I am sure this can be refined with more testing, and cooler heads. But this method works perfectly fine, and does exactly what I needed(to shuffle a List).

public List shufflelist(List list)
{
LetterClass tempelement;
List templist = new List();
List listcopy = new List();
int rand;

``````    foreach (LetterClass item in list)
{
}

while (listcopy.Count != 0)
{
rand = Random.Range(0, listcopy.Count);
tempelement = listcopy[rand];
listcopy.Remove(tempelement);

}

return templist;

}
``````

A quick google search yielded this: c# - Randomize a List<T> - Stack Overflow

after a little search this is how I got mine to work…
Maybe it helps somebody else ``````void Shuffle(List<GameObject> a)
{
// Loop array
for (int i = a.Count - 1; i > 0; i--)
{
// Randomize a number between 0 and i (so that the range decreases each time)
int rnd = UnityEngine.Random.Range(0, i);

// Save the value of the current i, otherwise it'll overwrite when we swap the values
GameObject temp = a*;*
``````

// Swap the new and old values
a = a[rnd];
a[rnd] = temp;
}

// Print
for (int i = 0; i < a.Count; i++)
{
Debug.Log(a*);*
}
}