473,386 Members | 1,828 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Need some help Please

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
4 1622
Ganon11
3,652 Expert 2GB
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
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
r035198x
13,262 8TB
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
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

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

Similar topics

6
by: mike | last post by:
Hello, After trying to validate this page for a couple of days now I was wondering if someone might be able to help me out. Below is a list of snippets where I am having the errors. 1. Line 334,...
5
by: John Flynn | last post by:
hi all i'm going to be quick i have an assignment due which i have no idea how to do. i work full time so i dont have the time to learn it and its due date has crept up on me .. As follows:...
5
by: TrvlOrm | last post by:
HI There, I have been struggling with JavaScript code for days now, and this is my last resort! Please help... I am trying to create a JavaScript slide show with links for Next Slide,...
7
by: Timothy Shih | last post by:
Hi, I am trying to figure out how to use unmanaged code using P/Invoke. I wrote a simple function which takes in 2 buffers (one a byte buffer, one a char buffer) and copies the contents of the byte...
15
by: Cheryl Langdon | last post by:
Hello everyone, This is my first attempt at getting help in this manner. Please forgive me if this is an inappropriate request. I suddenly find myself in urgent need of instruction on how to...
8
by: skumar434 | last post by:
i need to store the data from a data base in to structure .............the problem is like this ....suppose there is a data base which stores the sequence no and item type etc ...but i need only...
8
by: CM | last post by:
Hi, Could anyone please help me? I am completing my Master's Degree and need to reproduce a Webpage in Word. Aspects of the page are lost and some of the text goes. I would really appreciate it....
0
by: U S Contractors Offering Service A Non-profit | last post by:
Brilliant technology helping those most in need Inbox Reply U S Contractors Offering Service A Non-profit show details 10:37 pm (1 hour ago) Brilliant technology helping those most in need ...
21
by: asif929 | last post by:
I need immediate help in writing a function program. I have to write a program in functions and use array to store them. I am not familiar with functions and i tried to create it but i fails to...
5
by: Justin | last post by:
Here's my XML: <?xml version="1.0" ?> <AppMode Type="Network"> <CurrentFolder Path="c:\tabs"> <Tabs> <FilePath>tabs\Justin.tab</FilePath> <FilePath>tabs\Julie.tab</FilePath> *****There could...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.