I'm working on this program for class where I have to an amount of numbers that are randomly picked from 0 - 50. So what I have done is say there are 20 slots in my array so I'm trying to generator 20 random numbers for the array between 0-50 however I've ran into a few problems. Mainly figuring out how to make it print 20 seperate completely random numbers into the array. So here is what I have any help will be greatly appreciated. Thank you.
import java.util.Random;
public class Prog7_1
{
public static void main (String[] args)
{
int NUMBERS = 20, random, num;
Random generator = new Random();
{
for(random = 0; random < 20);
num = generator.nextInt(51);
random++;
}
int[] array = new int [NUMBERS];
for(int index = 0; index < NUMBERS; index++)
array[index] = num;
for(int value : array)
System.out.print(value + " ");
}
}
I'm guessing I have 2 problems 1. lays within array[index] = num; I've tried a few things to make it print out each seperate random number such as num++ but that just prints the first random number then adds 1 to it 20 times. Then second problem I'm guessing is with the loop that is making hte random numbers. Again thanks for any help.
I also just made a similar change with a while loop instead of a for loop and the program compiles but it just sits there when I run it and does nothing here is hte code I used for that one maybe that one is closer to my final program.
import java.util.Random;
public class Prog7_1
{
public static void main (String[] args)
{
int NUMBERS = 20, random = 0, num;
Random generator = new Random();
while(random < NUMBERS);
num = generator.nextInt(51);
//for(random = 0; random < 20);
//num = generator.nextInt(51);
//random++;
int[] array = new int [NUMBERS];
for(int index = 0; index < NUMBERS; index++)
array[index] = num;
random++;
for(int value : array)
System.out.print(value + " ");
}
}