Speeding up Random number list generator

Hi there,

I want to speed up my random number list generator because I've noticed that it is running really slow once I try to generate a list of over 20000 numbers. They all have to be unique and in random order.

So far, my code seems to work pretty fast with about 10000 numbers, but anything higher and it takes a bit too long to process.

I'm wondering if there is another way to speed up this process.

Attached is my code that I have.
private List<int> OutputRandomNumbers(int maxNumber)
{
    lblTextOutput.Text = "";
    List<int> numbers = new List<int>(maxNumber);

    //Add each number to the list
    for(int i = 1; i <= maxNumber; i++){ numbers.Add(i); }

    //Re-Order the list of numbers
    Random rand = new Random();
    int index = numbers.Count;
    while(index > 0)
    {
        index--;
        int randomIndex = rand.Next(1, numbers.Count);
        int tempVal = numbers[randomIndex];
        numbers[randomIndex] = numbers[index];
        numbers[index] = tempVal;
        lblTextOutput.Text += tempVal + (index > 1 ? ", " : "");
    }
    return numbers;
}

Open in new window

Q-MAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

EDDYKTCommented:
the slow part is when you try to join all string together
ie.
lblTextOutput.Text += tempVal + (index > 1 ? ", " : "");


to speed it up, use stringbuilder

private List<int> OutputRandomNumbers(int maxNumber)
        {
            StringBuilder sb = new StringBuilder();
            lblTextOutput.Text = "";
            List<int> numbers = new List<int>(maxNumber);

            //Add each number to the list
            for(int i = 1; i <= maxNumber; i++){ numbers.Add(i); }

            //Re-Order the list of numbers
            Random rand = new Random();
            int index = numbers.Count;
            while(index > 0)
            {
                index--;
                int randomIndex = rand.Next(1, numbers.Count);
                int tempVal = numbers[randomIndex];
                numbers[randomIndex] = numbers[index];
                numbers[index] = tempVal;
                sb.Append(tempVal + (index > 1 ? ", " : ""));
            }

            lblTextOutput.Text = sb.ToString();
            return numbers;
        }
James AtkinSenior Principle Software EngineerCommented:
How about something like this:
private List<int> OutputRandomNumbers(int maxNumber)
{
    lblTextOutput.Text = "";
    List<int> numbers = new List<int>(maxNumber);

    //Add each number to the list
    for (int i = 1; i <= maxNumber; i++) { numbers.Add(i); }

    //Re-Order the list of numbers
    Random rand = new Random();
    int index = numbers.Count;
    while (index > 0)
    {
        index--;
        int randomIndex = rand.Next(1, numbers.Count);
        int tempVal = numbers[randomIndex];
        numbers[randomIndex] = numbers[index];
        numbers[index] = tempVal;
    }
    string idString = String.Join(",", numbers);
    lblTextOutput.Text = idString;
    return numbers;
}

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Q-MAuthor Commented:
Thanks guys, I used String.Join and worked great. Runs instantly.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.