You post did not clearly state what error you received or how your output differs from that expected by the assignment. It looks to me that you are not yet printing the graphical representation of the occurrence of the numbers.
What is the purpose of your contraction of the range of your random numbers?
Why do you use the "RandUtils" class?
When I need a random number generator I usually use an instance of Random, such as:
Random myRandomGen = new Random();
or
Random myRandomGen = new Random( time ); // using current time as a seed value
When I need an int from a range of ints which has a ceiling of a particular int, I use an instruction such as:
nextInt = myRandomGen.nextInt( 6 );
which will produce an int between 0 (inclusive) and 6 (exclusive). This will randomly generate ints 0, 1, 2, 3, 4, or 5.
NOTE: THE FULL CODE IS AT THE BOTTOM OF THS POST, BUTBEFORE YOU GET THERE, I'LL TALK YOU THROUGH IT
I'm away from my home computer right now, and have no access to a java compiler, so this will be full of bad syntax, but no doubt you can change it a little to make it work.
To make a random number, all you need to do is
Code:
int randomInt=0; //creates and initialises an integer to store the random number
Random myNewRandom = new Random(); //creates a new instance of the random class
randomInt = myNewRandom.nextInt(5); //generates a new random number between 0 and 5
now, to add up how many times each number occurs, you will need counters. these can easily be achieved with 6 integers and either a selection of If/Else statements, or Switch statements. I'm a fan of If/Else over Switch, so thats how I'm writing this part
Code:
(declare the integers 0count, 1count etc at the very start of the program)
If (randomInt == 0) {
0count=0count+1; //adds 1 to the counter for 0
}
else if (randomInt == 1) {
1count=1count+1; //adds 1 to the counter for 1
}
you'll need to make an "else if" for all the other possibilities as well, all the way to "else if (randomInt == 5)"
now, to get it to loop 100 times, you will need a "for loop".
Code:
for (int counter=1; counter<=100, counter++)
{
//insert code here
}
That should loop 100 times. It may loop 99 times, in which case change the first bit to "int counter=0;"
so, in some basic pseudocode, it should look like this:
Code:
int count0=0;
int count1=0;
int count2=0;
int count3=0;
int count4=0;
int count5=0;
randomInt=0;
Random myNewRandom = new Random();
for (loop 100 times)
{
generate a new random number using "randomInt = myNewRandom.nextInt(5);
use the if statements to check what was output and add 1 to the correct total
}//end for loop
then use some System.out.println to output the counters.
to get the graphs to appear, the easiest way I can think of doing it is to use a seperate method, let's call it "drawGraph", and it takes an integer.
so it's declared as:
Code:
public static void drawGraph(int output){
}
and inside the method, have this code:
Code:
System.out.print(output); //starts a line with the current number
for (int count = 0; count <output; count++)
{
System.out.print("-"); //important that you use .print instead of .println, this will make a line with one less -'s as required
}
System.out.println("+"); //caps the line with a + and starts a new line
to use this method, go back to the main method, and use the following six lines at the end:
Code:
drawGraph(count0); //draws the graph line for the counter of 0
drawGraph(count1); //draws the graph line for the counter of 1
drawGraph(count2); //draws the graph line for the counter of 2
drawGraph(count3); //draws the graph line for the counter of 3
drawGraph(count4); //draws the graph line for the counter of 4
drawGraph(count5); //draws the graph line for the counter of 5
this will pass the values of the counter through to the drawGraph method, which outputs the graph.
So, off the top of my head, the following is what I think the code should be:
Code:
import java.util.*;
public class RandDemo{
public static void main(String args[] ){
int zerocount=0;
int onecount=0;
int twocount=0;
int threecount=0;
int fourcount=0;
int fivecount=0;
int randomInt=0;
Random myNewRandom = new Random();
System.out.println("Student ID: 0400185");
for (int counter = 1; counter <=100; counter++)
{
randomInt=myNewRandom.nextInt(6);
if (randomInt == 0) {
zerocount=zerocount+1; //adds 1 to the counter for 0
}
else if (randomInt == 1) {
onecount=onecount+1; //adds 1 to the counter for 1
}
else if (randomInt == 2) {
twocount=twocount+1; //adds 1 to the counter for 2
}
else if (randomInt == 3) {
threecount=threecount+1; //adds 1 to the counter for 3
}
else if (randomInt == 4) {
fourcount=fourcount+1; //adds 1 to the counter for 4
}
else if (randomInt == 5) {
fivecount=fivecount+1; //adds 1 to the counter for 5
} //end if
} //end for loop
for (int i=0; i<3; i++)
{
System.out.print("0123456789");
}//end for loop
System.out.println(""); //prints a clear line
System.out.println("0 > "+zerocount);
System.out.println("1 > "+onecount);
System.out.println("2 > "+twocount);
System.out.println("3 > "+threecount);
System.out.println("4 > "+fourcount);
System.out.println("5 > "+fivecount);
drawGraph(zerocount, 0);
drawGraph(onecount, 1);
drawGraph(twocount, 2);
drawGraph(threecount, 3);
drawGraph(fourcount, 4);
drawGraph(fivecount,5);
}//end main method
public static void drawGraph(int output, int number){
System.out.print(number + ": "); //starts a line with the current number
for (int count = 0; count <output; count++)
{
System.out.print("-"); //important that you use .print instead of .println, this will make a line with one less -'s as required
}//end for
System.out.println("+"); //caps the line with a + and starts a new line
}//end drawGraph method
}//end of class
Feel free to corrcet any syntax or errors, and check the values used i nthe for loop. Like I said, right now I have no access to a java compiler, so I can't check if this will compile and run nicely without some small fixes
EDIT: got hold of a compiler, and my code gave me this output:
Bookmarks