[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 409
  • Last Modified:

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

0
Q-M
Asked:
Q-M
2 Solutions
 
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
 
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
 
Q-MAuthor Commented:
Thanks guys, I used String.Join and worked great. Runs instantly.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now