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?
 
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

0
 
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;
        }
0
 
Q-MAuthor Commented:
Thanks guys, I used String.Join and worked great. Runs instantly.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.