# Array random numbers

• July 20th, 2011, 03:10 PM
nitwit3
Array random numbers
Hi
I was just wondering if someone could help me a bit in my java code.
Basically what iv done is made a program that chooses 15 numbers between 1 and 100.
I was wondering if there was an easier way of writting each array for these fifteen numbers.
As of now i have written each code individually like this:

Code :

``` //Array Numbers Random randomGenerator = new Random (); int num[] = new int[16]; for (int idx = 1 ; idx < 16 ; idx++) { num[idx] = (int)(Math.random()*100); } c.println("Here are 15 random numbers between 1 and 100:"); c.println (num[1]); c.println (num[2]); c.println (num[3]); c.println (num[4]); c.println (num[5]); c.println (num[6]); c.println (num[7]); c.println (num[8]); c.println (num[9]); c.println (num[10]); c.println (num[11]); c.println (num[12]); c.println (num[13]); c.println (num[14]); c.println (num[15]); }```

The program goes work, i was just interested in finding out whether there was an easier way to put these random number.
• July 20th, 2011, 04:05 PM
Norm
Re: Array random numbers
Yes, look at using a for loop. Replace the hardcoded indexes [0] etc with [i] where i is the for loop index.
• July 20th, 2011, 04:15 PM
nitwit3
Re: Array random numbers
Thank You!
• July 20th, 2011, 06:36 PM
Junky
Re: Array random numbers
I have to wonder how you can think to use a loop to insert values into an array but couldn't think to use a loop to print them out.

Code java:

`int num[] = new int[16];`
If you want an array to hold N number of things then create the array with a size of N. Do not create it with a size of N+1 just to avoid using index 0.
• July 20th, 2011, 07:05 PM
nitwit3
Re: Array random numbers
yea i know .. im not the brightest light bulb out there -____-