How can I shuffle present items in a listBox randomly?
I have this code, but it is very slow and inefficient -
private void shuffleItemsToolStripMenuItem_Click(object sender, EventArgs e)
{
ListBox.ObjectCollection list = listBox1.Items;
Random rng = new Random();
int n = list.Count;
while ( n > 1 )
{
n--;
int k = rng.Next(n + 1);
string value = (string)list[k];
list[k] = list[n];
list[n] = value;
}
}
While this code does technically work, it's very slow and doesn't give many variations, often giving the same few shuffles.
I've been searching on Google for the last hour and have been unable to find a solution to this. I am running out of hair to pull.
Thanks.