By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,855 Members | 865 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,855 IT Pros & Developers. It's quick & easy.

Need some help Please

P: 5
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 + " ");


}
}
Feb 15 '07 #1
Share this Question
Share on Google+
4 Replies


Ganon11
Expert 2.5K+
P: 3,652
Expand|Select|Wrap|Line Numbers
  1. import java.util.Random;
  2.  
  3. public class Prog7_1
  4. {
  5.     public static void main (String[] args)
  6.     {
  7.             int NUMBERS = 20, random, num; 
  8.  
  9.             Random generator = new Random();
  10.             { // What is this bracket doing here?
  11.             for(random = 0; random < 20); // Semicolon following the for statement will result in a pointless loop - in this case, an infinite loop, as random++ is outside the header.
  12.                 num = generator.nextInt(51); 
  13.                         // You generate num, but you never do anything with it!
  14.                 random++; // Should be in the header.
  15.             }
  16.             int[] array = new int [NUMBERS];
  17.  
  18.             for(int index = 0; index < NUMBERS; index++)
  19.             array[index] = num; // This will set each element of the array to the same value
  20.  
  21.             for(int value : array) // This is a bad header that I'm not sure I understand.
  22.             System.out.print(value + "  "); // All you're doing here is printing value, not the value of the array.
  23.     }
  24. }
The above code needs some serious surgery. I think what you're trying to do can be accomplished in the following steps:

1) Declare variables - int num, final int NUMBERS, and int[] array.
2) for...loop from 0 to < NUMBERS (index i)
2) a. Generate a random number from 0 to 50 - store into num
2) b. Set the ith member of array to num (array[i] = num;).
2) c. Output array[i].

This is all that the above code was trying to accomplish, I think.

If you have to have unique random numbers, you can add the following logic:

2) d. for...loop from 0 to < i (index j)
2) d. 1. if array[j] == array[i], you have a repeat - you should generate a new random number. You can 'reset' this step of the array by decrementing i (i--) and then breaking from the second for... loop. The 1st loop will begin generating a random number for the same element.
Feb 16 '07 #2

P: 5
The above code needs some serious surgery. I think what you're trying to do can be accomplished in the following steps:

1) Declare variables - int num, final int NUMBERS, and int[] array.
2) for...loop from 0 to < NUMBERS (index i)
2) a. Generate a random number from 0 to 50 - store into num
2) b. Set the ith member of array to num (array[i] = num;).
2) c. Output array[i].

This is all that the above code was trying to accomplish, I think.

If you have to have unique random numbers, you can add the following logic:

2) d. for...loop from 0 to < i (index j)
2) d. 1. if array[j] == array[i], you have a repeat - you should generate a new random number. You can 'reset' this step of the array by decrementing i (i--) and then breaking from the second for... loop. The 1st loop will begin generating a random number for the same element.[/quote]

For some reason whenever I set the first for loop 0 < 20 or random < NUMBERS i always get an error saying its expecting a ">" which i don't understand being that won't work cause 0 can't be greater then 20. Programming just kicks my ass lol.
Feb 16 '07 #3

10K+
P: 13,264
The above code needs some serious surgery. I think what you're trying to do can be accomplished in the following steps:

1) Declare variables - int num, final int NUMBERS, and int[] array.
2) for...loop from 0 to < NUMBERS (index i)
2) a. Generate a random number from 0 to 50 - store into num
2) b. Set the ith member of array to num (array[i] = num;).
2) c. Output array[i].

This is all that the above code was trying to accomplish, I think.

If you have to have unique random numbers, you can add the following logic:

2) d. for...loop from 0 to < i (index j)
2) d. 1. if array[j] == array[i], you have a repeat - you should generate a new random number. You can 'reset' this step of the array by decrementing i (i--) and then breaking from the second for... loop. The 1st loop will begin generating a random number for the same element.

For some reason whenever I set the first for loop 0 < 20 or random < NUMBERS i always get an error saying its expecting a ">" which i don't understand being that won't work cause 0 can't be greater then 20. Programming just kicks my ass lol.
Break it up. Write a method calles contains
public boolean contains(ArrayList array, int value) {
which returns true if value is a member of array.

Now your loop should look like this
Expand|Select|Wrap|Line Numbers
  1.  int index = 0; 
  2. while(index < 20) {
  3.         int x = (int)(Math.random() * 50);
  4.         while(contains(array, x)) {
  5.              x = (int)(Math.random() * 50);
  6.         }
  7.         array[index++] = x;
  8. }
  9.  
Feb 16 '07 #4

P: 5
Thanks for your help I still wasn't able to get it lol I don't think i'll ever survive this java class but it was due last night and I just turned it what I had. Thanks though for helpin.
Feb 16 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.