# Making a random array of integers

• 11-08-2007
Vidak
Making a random array of integers
Hi,

Yesterday I switched from Delphi to C# and I'm very unfamiliar with it's syntax and all. So, I wanted to make a program which produces an array of 100 random integers, and then writes them down in a listbox.

Well I somehow managed to make this code, with lots of googling about how to generate random numbers, delaring arrays, loops etc.

The problem is, my program just generates 2 numbers, and repeats them. Please help me, I know this is a very newb question, but I'm so ........ed off about it, can't find what's wrong.

And the random function really sux in c# :S

Here is the code:

Code:

```private void button1_Click(object sender, EventArgs e)         {           int [] niz = new int[100];                     for (int i = 0; i < 100; i++)           {                             Random m = new Random();               int ZOMG = m.Next(0,100);               niz[i] = ZOMG;               listBox1.Items.Add(Convert.ToString(ZOMG));               i = i + 1;                         }         }```
• 11-08-2007
Declare your Random object outside of the loop. Since the random seed is stored inside the Random object, recreating it will reset the random seed.
(The random seed is a number that is used to make up a random number, the same random number will be returned for the same random seed)

Quote:

And the random function really sux in c# :S
Its not the function that sux (actually its even an object), its your use of it :D ;)
• 11-09-2007
DanFraser
If I may step in and make some constructive comments...

Code:

```private void button1_Click(object sender, EventArgs e)         {           //this is the initialisation of what you had in the loop as Raven mentioned.  As a rule never try to initialise in a loop.           int [] niz = new int[100];           Random m = new Random();                               for (int i = 0; i < 100; i++)           {                             Random m = new Random();//so this disappears               int ZOMG = m.Next(0,100);//remove this line               niz[i] = ZOMG;//ZOMG is a fluffy extra variable you don't need.  Do the line below ... niz[i] = m.Next(0,100); ... //this just puts the random number straight in to your array.               listBox1.Items.Add(Convert.ToString(niz[i]));//ZOMG changed to niz[i]               i = i + 1;//not needed, the for loop increments 'i' by one anyway                         }         }```
Now I am probably going a bit too far, but here's the clean version -

Code:

```private void button1_Click(object sender, EventArgs e) {   int [] niz = new int[100];   Random m = new Random();   for (int i = 0; i < 100; i++)   {       niz[i] = m.Next(0,100);       listBox1.Items.Add(Convert.ToString(niz[i]));         } }```
However, just as I started this in notepad++ so I could see what I was doing, I realised this is the fastest way, just throw the random number straight in to the actual listbox itself! The reason being niz[i] is not actually available to any other part of the program. I am not clear on whether you need to use niz[i] anywhere else though, but this next bit assumes you don't:

Code:

```private void button1_Click(object sender, EventArgs e) {         Random m = new Random();         for (int i = 0; i < 100; i++)         {                 listBox1.Items.Add( Convert.ToString( m.Next(0,100) ) );         } }```
If you do need to access niz[i], you could easily just do the last bit of code I gave, but refer to the listbox location you want to look at.

so instead of calling say :

Code:

`    randomVariableName = niz[randomNumber];`
this would allow you to call the number directly from the listbox :

Code:

`    randomVariableName = listBox1.Items.IndexOf(randomVariableGetter);`
There are many ways of grabbing something from a listbox, I believe that is one of them.

I hope I have been some help!