Solved

# Speeding up Random number list generator

Posted on 2011-10-04
402 Views
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;
}
``````
0
Question by:Q-M

LVL 26

Assisted Solution

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

LVL 4

Accepted Solution

``````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;
}
``````
0

Author Comment

Thanks guys, I used String.Join and worked great. Runs instantly.
0

## Featured Post

Okay. So what exactly is the problem here? How often have we come across situations where we need to know if two strings are 'similar' but not necessarily the same? I have, plenty of times. Until recently, I thought any functionality like that wo…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…