Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Randomize a Queue

Tags:

c#

I read lines from txt file. They are about 100 000. How to fill queue and shuffle its elements? Fill queue like this:

    Queue<string> accs = new Queue<string>();
    private void loadLikeAccountsToolStripMenuItem_Click(object sender, EventArgs e)
    {
        OpenFileDialog openFileDialog1 = new OpenFileDialog();
        openFileDialog1.RestoreDirectory = true;
        openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
        if (openFileDialog1.ShowDialog() == DialogResult.OK)
        {
            accs.Clear();

            foreach (string s in File.ReadAllLines(openFileDialog1.FileName))
            {
                accs.Enqueue(s);
            }
            label4.Text = accs.Count.ToString();
        }
    }
like image 313
Kenji Avatar asked May 29 '26 04:05

Kenji


1 Answers

A queue is for FIFO. You're asking for something other than FIFO. So, you're using the wrong tool for the job.

A simple approach is instead of filling a queue, fill a list, and then shuffle the elements of the list.

like image 59
jason Avatar answered May 31 '26 19:05

jason



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!