# Thread: hard question .. help me .. about creating random numbers

1. Member
Join Date
Dec 2009
Posts
2
Rep Power
0

## hard question .. help me .. about creating random numbers

I want a program that create an array of random numbers between 1 and 50 , and prints a histogram which display how many integers are in each 'decade' ( use a star for each decade ), as it shown down:

the output
-------------------------------------------------------------
How many random numbers?
20

6 26 48 13 48 28 10 28 32 45 18 21 3 7 30 9 38 3 36 15
1-10 |******
11-20 |***
21-30 |****
31-40 |****
41-50 |***
--------------------------------------------------------------

plz help me I'll be thankful
:)

2. If this link doesn't help, then please post your best "good faith" attempt at solving this problem followed by a few specific questions (your original post above doesn't in fact have any questions, much less specific ones). Usually the more specific your question, the better we'll be able to help you.

Again, much luck.

3. Member
Join Date
Dec 2009
Posts
2
Rep Power
0
thanks dear I write this program
Java Code:
```import java.io.*;
import java.lang.*;
import java.util.Random;

class Test
{
public static void main(String[] args) throws Exception
{
System.out.println("How many random numbers");
z=scan.nextInt();
for(int i=1; i<z; ++i)
{
System.out.print(" " + (new Random().nextInt(1-50)+50));
}
}
}```
it's not complete

I want a program that ask me to enter # number then the program will print a line of # integer random numbers like that
Java Code:
`6 26 48 13 48 28 10 28 32 45 18 21 3 7 30 9 38 3 36 15`
then the program will organize this # random numbers and divided it into 5 groups each group contain 10 numbers group number 1 from (1-10) ,numbers 2 from (11-20) , group numbers 2 from (21-30) group number 1 from (31-40) , group number 1 from (41-50) and and calculate how many number in each group if it's 1 number , the program will print for ex group number 1:
Java Code:
`1-10 |*`
if it's 2 numbers , the program will print
Java Code:
`1-10 |**`
so the out put of the program is like that
the output
-------------------------------------------------------------
Java Code:
`How many random numbers?`
-------------------------------------------------------------
after entering a number for exp. "20" the output will be
Java Code:
```How many random numbers?
20

6 26 48 13 48 28 10 28 32 45 18 21 3 7 30 9 38 3 36 15
1-10 |******
11-20 |***
21-30 |****
31-40 |****
41-50 |***```
-------------------------------------------------------------
look [6 26 48 13 48 28 10 28 32 45 18 21 3 7 30 9 38 3 36 15] are random numbers..

plz help me I want to submit my assignment tomorrow..
my regards
Last edited by soldier; 12-20-2009 at 11:09 PM.

4. 1) Check your for loop
Java Code:
`for(int i=1; i<z; ++i)`
as I think that you want it to start at 0, not at 1. Try it out for small numbers. The reason is that your loop continues as long as i is < z meaning it will currently go from 1 to z-1 which is z - 1 times. You want it to loop z times and so it must go from i = 0 to i < z.

2) You need to do more than just get your random numbers and display them. You also have to analyze them, and so to do that, you'll need to store them. I suggest that you create an int array of size z for this purpose. Shoot, your instructions tell you explicitly to do this, so I guess it's a good idea!

3) I'm not sure if you're allowed repeats or not, but if not, you'll need to check each random number to see if it is already in the array (loop through the array for this), and if so, get another random number. You would need a loop to do this.

4) To see how many of each number is present in each "decade", I suggest that you create another array int[5] of ints, one for each decade. Then after your random number array has been created, loop through it incrementing the proper item in the second array depending on which decade the random array item is.

5) To draw your *, you'll need to use a for loop that print("*") and uses this second array to decide how many times to loop.

The key here is to break the big problem up into small problems. I suggest you work out each small issue one at a time and in isolation if possible before trying to combine it all.

Much luck!

5. Member
Join Date
Dec 2009
Location
Rio de Janeiro
Posts
38
Rep Power
0
If you have a fixed number of random numbers, you can create a list and shuffle it.

Java Code:
```public class Numbers{

static List<Integer> lstNumber = new ArrayList<Integer>();

public static void main(String[]args)throws Exception{
final int MAXNUMBERS = 50;

for(int i = 0; i < MAXNUMBERS; i++){
}

Collections.shuffle(lstNumber);

System.out.println("How many random numbers? ");
int numberRandon = Integer.parseInt(new Scanner(System.in).nextLine());

for(int i = 0; i < numberRandon; i++){
System.out.println(lstNumber.get(i));
}
}
}```

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•