generating random integers
So far in my java programming class i have been doing well, but i have a new assignment that is due this friday october 19th i've been trying to figure it out for many hours now and i am stumped. we are using eclipse IDE. this is my assignment:
Problem to Solve
This assignment is to solve problem using loops. Write a program to generate 100 random integers
in the range of 0 and 99999, and the frequency count of each digit (0, 1, 2, . . . , 9) in these numbers,
and print a frequency histogram.
How to Do It:
The Overall program structure may look like this:
1. Use ten variables for the frequency counts for the ten digits 0, 1, 2, . . . , 9.
2. Other variables are also needed. For example, a variable to hold a random number, a variable
for the digit, etc.
3. Repeat 100 times (a loop):
Generate a random number.
For each digit d in the number, increase the frequency count of d by 1. This is done
using a loop. Note: we do not know how many digits are in the number.
4. For each digit from 0 to 9 (a loop):
Store its frequency count into a variable.
Display the digit and the frequency count.
Display the horizontal frequency bar of asterisks using a loop.
To accomplish the task, Here are some suggestions:
1. Generate random integers in the range [0, 99999]. Assume the variable is number:
number = (int)(Math.random() * 99999);
2. Get the digits of the random number. Since we do not know how many digits in the number,
you need to use a loop to "peel off" the rightmost digit one at a time using integer division
(/) and modulus (%) operations.
i need help on how to begin this program
if anyone can help me i will be much appreciative of it because i have been very very busy and stressed lately and i need these points for this assignment or at least some points.
thank you,
-Alex
Re: generating random integers
Quote:
Originally Posted by
javaFREEK
i need help on how to begin this program
The same way you start any other program: import statements followed by class header. Actually the best place to start is with a piece of paper and start sketching out some ideas of what variables you will need, what methods you will need and the general flow of the program.
Quote:
if anyone can help me i will be much appreciative of it because i have been very very busy and stressed lately and i need these points for this assignment or at least some points.
People are willing to help but you need to make it easy as possible for us. As you post stands it is simply a "Here's my assignment. Give me the code" type of post.
If you want our help you need to make an attempt. Write some code. Post it here and include full error messages if you get them. Most importantly of all - ask a specific question. "I don't know how to do me assignment" is not specific and not a question.
Re: generating random integers
BTW saying that you are reaaaaaaaally busy is your problem not ours. We all have our own issues/workloads and must manage them. Don't impose your timeframes on us.
Re: generating random integers
public class project6 {
private static Scanner stdin;
public static void main(String[] args) {
stdin = new Scanner(System.in);
int number;
int freq0 = 0, freq1, freq2, freq3, freq4, freq5, freq6, freq7, freq8, freq9;
number = (int)(Math.random() * 99999);
switch(number){
case 1 : System.out.println("*" + freq0);
}
}
}
}
}
this is what i have and i do not know what to do i need the program to generate a random number and count the frequency of each digit then display asterisk of how many times the digit occurs.
Re: generating random integers
now i have this
public class project6 {
private static Scanner stdin;
public static void main(String[] args) {
stdin = new Scanner(System.in);
int number, digit, min = 0;
int freq0 = 0, freq1 = 1, freq2 = 2, freq3, freq4, freq5, freq6, freq7, freq8, freq9;
//display random number.
number = (int)(Math.random() * 99999);
System.out.println(number);
//display digits of random number.
}
}
Re: generating random integers
how do i display the digits of the number?
Re: generating random integers
Quote:
Originally Posted by
javaFREEK
how do i display the digits of the number?
System.out.println(number.length);
:D
Re: generating random integers
You can use java.util.Random class's nextInt() method or nextInt(int range);
See [Blog spam removed] for other data types.