Making a random array of integers

This is a discussion on Making a random array of integers within the C# Programming forums, part of the General Programming Boards category; Hi, Yesterday I switched from Delphi to C# and I'm very unfamiliar with it's syntax and all. So, I wanted ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    1

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

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    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)

    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
    Last edited by Raven Arkadon; 11-08-2007 at 04:47 PM.
    signature under construction

  3. #3
    Registered User
    Join Date
    Jun 2003
    Posts
    129
    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!
    Last edited by DanFraser; 11-09-2007 at 06:17 AM. Reason: Even more clarification
    He who asks is a fool for five minutes, but he who does not ask remains a fool forever.

    The fool wonders, the wise man asks. - Benjamin Disraeli

    There are no foolish questions and no man becomes a fool until he has stopped asking questions. Charles Steinmetz

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with Dynamically Increasing Array of Integers
    By laserlight in forum C++ Programming
    Replies: 30
    Last Post: 07-04-2008, 08:27 AM
  2. Using unix command line to sort random array
    By lostmyshadow in forum C Programming
    Replies: 4
    Last Post: 12-11-2007, 07:14 PM
  3. Replies: 4
    Last Post: 11-16-2004, 07:29 AM
  4. making an empty 2-D array
    By starX in forum C Programming
    Replies: 4
    Last Post: 02-08-2002, 01:09 AM
  5. making an array name a variable
    By Zaarin in forum C++ Programming
    Replies: 5
    Last Post: 09-02-2001, 07:17 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21