Hi guys,

This week Ive been wrecking my head trying to write a code to run the following program:


This program must test the dice 50 times. Then the results will be recorded and summarised. For each number (1 to 6), the program will print the most times that number occurred, the fewest times it occurred and the average.

The program output would look like:


How many times do you want to throw the dice:
6000
Number Average Most Fewest
1: 1001.3 1011 987
2: 998.3 1002 999
3: 1001.8 1020 987
4: 1000.9 1010 983
5: 997.9 1009 981
6: 999.3 1015 987


Basically, the program will ask the user how many times they want to roll a dice, then that dice will be rolled and the amount of times it lands on a number will be recorded i.e 6 occurred 20 times etc.


Then this code will be tested 50 times and when that is done it will print the maximum times a number was recorded, the minimum times and the average for all 50 tests.

So far I managaed to write a code that will roll the dice and record the amount of times a certian number appears but I have no clue how to even try the next part.

I am to try and use methods and arrys for this code but I just cant seem to think of anything.

Here is my code so far

import java.util.Random;

public class RandomDie
{
	
		

		
	public static void main(String [] args)
	{
		System.out.println("How many times do you want to roll the dice?");
		int y = Console.readInt();
	
	
	
		int x = 6 + 1;	
	
		Random generator = new Random();
	
		int[] count = new int[x];


		for(int i = 1; i < y ; i++)
		{	 
			int d = generator.nextInt(x);
			count[d]++;        	
		}
	
		for(int p = 1; p < x; p++)
		{
			System.out.println(p  + " " + count[p]);
		}
		
	}
}

Any ideas or hints would be greatly appreciated guys.

Thanks so much in advance.
-David

Recommended Answers

All 21 Replies

You need two main loops for this program. The outer one runs the test inside 50 times. The inner one goes the number of times the user asks for.

I would use 4 arrays with 6 elements each for this problem. The first array stores the number of dice rolls in the inner loop. The second stores the most numbers and the third stores the fewest. The last array stores the sum of all the rolls from the first array. You will use this last one to calculate the averages. All but the first array will be filled after the inner loop executes, but before the outer loop ends. Then you need to reset the first array. The structure of your program will look something like this:

Declare the 4 arrays.
Prompt the user for the number of dice rolls.
Initialize each element in most array to -1.
Initialize each element in fewest array to number (from user) +1.
Begin loop that goes 50 times
   Begin loop that goes number (from user) times
      Roll the die
      increment appropriate element in first array according to die roll
   End loop that goes number (from user) times
   For each number in first array (this is another loop)
      If it is higher than corresponding number in most array,
         Copy the number from first array into most array.
      If it is lower than corresponding number in fewest array,
         Copy the number from first array into fewest array.
      Add it to corresponding number in the last array that holds the sums.
   End of for each loop
   Reset first array to all 0's.
End loop that goes 50 times.
Calculate averages
Print results

Thanks so much for the help man.

How would I initialize a array to a value? Like when you say initialize the most array to -1?

Thanks again dude:)

Do you know how to write a for loop? This is the easiest way to initialize the array.

Yeah I know how to write for loops, maybe something along the lines of

for(int i = 0; i <= most.length; i++)
{
         most[]++;
}

Is that along the right lines?

Thanks again:)

Close. Just change line 4 to

most[i] = -1;

Ok how does this look so far?

int [] rolls = new int[6];
		int [] most = new int[6];
		int [] fewest = new int[6];
		int [] sum = new int[6];

		System.out.println("How many times would you like to roll the dice?");
		
		int y = Console.readInt();
		
		for(int i = 0; i <= most.length; i++)
		{
			most[i] = -1;
		}
		
		for(int z = 0; z <= fewest.lenght; z++)
		{
			fewest[y] = 1;
		}

1. On lines 13 and 18, make the loop condition be only less than (<) instead of <=.
2. Change line 20 to this: fewest[z] = y+1; .

Ok here is what Ive come up with

public class Random
{

	static int random()
	{
		int dice1 = (int)(Math.random()* 6 + 1);

		return dice1;
	}	
	
	public static void main(String [] args)
	{


		int [] rolls = new int[6];
		int [] most = new int[6];
		int [] fewest = new int[6];
		int [] sum = new int[6];

		System.out.println("How many times would you like to roll the dice?");
		
		int y = Console.readInt();
		
		for(int i = 0; i < most.length; i++)
		{
			most[i] = -1;
		}
		
		for(int z = 0; z < fewest.lenght; z++)
		{
			fewest[z] = y+1;
		}
			
		for(int p = 0; p <= 50; i ++)
		{
			for(int q = 0; q < y; q++)
			{
				
			}

		}
		
	}
}

Could you give me some insight about these two steps
#
Roll the die
#
increment appropriate element in first array according to die roll.

Since I have used a method to make my dice im unsre of how to roll it.

Thanks for putting up with me dude, I know im quite bad at this:P

OK. So "Roll the die" in your case means calling the random() method you wrote. However there is a slight complication. Normally you would want to the die roll to be a number between 1 and 6. The way you have written your random method, that's what it will return. But in Java, array indices start at 0, so your arrays actually go from 0-5. So to make things simple, I would remove the +1 on line 7.

Now think about this. Your rolls array has 6 slots (numbered 0 to 5) and your random() method will return a number in the range of 0 to 5 (after you remove the +1.) So how do you think you can increment the appropriate element in the rolls array based on the number that gets returned from the random() method?

Can you think of the answer?

public class Random
{

	static int random()
	{
		int dice1 = (int)(Math.random()* 6);

		return dice1;
	}	
	
	public static void main(String [] args)
	{


		int [] rolls = new int[6];
		int [] most = new int[6];
		int [] fewest = new int[6];
		int [] sum = new int[6];

		System.out.println("How many times would you like to roll the dice?");
		
		int y = Console.readInt();
		
		for(int i = 0; i < most.length; i++)
		{
			most[i] = -1;
		}
		
		for(int z = 0; z < fewest.lenght; z++)
		{
			fewest[z] = y+1;
		}
			
		for(int p = 0; p <= 50; i ++)
		{
			for(int q = 0; q < y; q++)
			{
				
			}

		}
		
	}
}

Ok so Ive taken out the plus 1 and I can see what your saying.

My idea would be to change the rolls array too

int [] rolls = new int [5];

No, the rolls array should still have 6 elements. They are just numbered 0-5.

Ok what if I add a + 1 to the return dice statement?

Like this

public class Random
{

	static int random()
	{
		int dice1 = (int)(Math.random()* 6);

		return dice1 + 1;
	}	
	
	public static void main(String [] args)
	{


		int [] rolls = new int[6];
		int [] most = new int[6];
		int [] fewest = new int[6];
		int [] sum = new int[6];

		System.out.println("How many times would you like to roll the dice?");
		
		int y = Console.readInt();
		
		for(int i = 0; i < most.length; i++)
		{
			most[i] = -1;
		}
		

		for(int z = 0; z < fewest.lenght; z++)
		{
			fewest[z] = y+1;
		}
		
	
		for(int p = 0; p <= 50; i ++)
		{
			for(int q = 0; q < y; q++)
			{
				random();
				rolls[random()]++;
			}

		}
		
	}
}

No, you don't want the +1 on the return statement either. As I said before, normally you would want the dice roll to be one of the numbers {1, 2, 3, 4, 5, 6}. But since in Java, arrays start at 0, we instead want the numbers to be {0, 1, 2, 3, 4, 5}. There are still 6 of them, we're just numbering them slightly differently. Now the number 0 really represents a dice roll of 1, and the number 1 really represents the dice roll of 2, etc.

So you need to make the following changes to the code just posted:

Change line 9 back to just return dice1;
Get rid of line 41.  It doesn't do anything.
Change <= on line 37 to just <.
Also on line 37, change i++ to p++.

So far so good.

Ok, the max and min part now using the if statements is really confusing me but this is what Ive managed so far

public class Random
{

	static int random()
	{
		int dice1 = (int)(Math.random()* 6 + 1);

		return dice1;
	}	
	
	public static void main(String [] args)
	{


		int [] rolls = new int[6];
		int [] most = new int[6];
		int [] fewest = new int[6];
		int [] sum = new int[6];

		System.out.println("How many times would you like to roll the dice?");
		
		int y = Console.readInt();
		
		for(int i = 0; i < most.length; i++)
		{
			most[i] = -1;
		}
		

		for(int z = 0; z < fewest.length; z++)
		{
			fewest[z] = y+1;
		}
		
	
		for(int p = 0; p < 50; p ++)
		{
			for(int q = 0; q < y; q++)
			{
				rolls[random()]++;
			}

		}
		for(int q = 0; q < rolls.length; q++)
		{	
			if(q > most[])
				most[]++;
			if(q < fewest[])
				fewest++;
		}
		
	}
}

OK, take one step at a time. How do you know if a given number in the rolls array is higher than the corresponding number in the most array? Can you just write that if statement?

Note you should never have an array reference with nothing in between the square brackets, except when you initially declare the array.

ok I think you might make a max int and then compare it to your array maybe?

And no I dont think I can just write it like that, I think id have to lose the brackets like this

for(int q = 0; q < rolls.length; q++)	
		if(q > most[])
			most[]++;
		
                if(q < fewest[])
		        fewest++;

If I cant leave the brackets blank, what would I put into them that wouldn't set it to one value?

You do need the curly braces. It's wrong now that you removed them. But you didn't change the if statement.

To compare if a given number in the rolls array is higher than the corresponding number in the most array, you need to do something like this:

if (rolls[q] > most[q])

Now if that's the case, you don't just want to do most[q]++; . You need to copy the value from the rolls array at the appropriate index (q) into the most array at that same index.

Sorry im only getting back to you now.

Here is what ive come up with

public class Random
{

	static int random()
	{
		int dice1 = (int)(Math.random()* 6 + 1);

		return dice1;
	}	
	
	public static void main(String [] args)
	{


		int [] rolls = new int[6];
		int [] most = new int[6];
		int [] fewest = new int[6];
		int [] sum = new int[6];

		System.out.println("How many times would you like to roll the dice?");
		
		int y = Console.readInt();
		
		for(int i = 0; i < most.length; i++)
		{
			most[i] = -1;
		}
		

		for(int z = 0; z < fewest.length; z++)
		{
			fewest[z] = y+1;
		}
		
	
		for(int p = 0; p < 50; p ++)
		{
			for(int q = 0; q < y; q++)
			{
				rolls[random()]++;
			}

		}
		for(int q = 0; q < rolls.length; q++)
		{	
			if(rolls[q] > most[q])
				rolls[q]++;
			if(rolls[q] < fewest[q])
				fewest[q]++;
		}
		
	}
}

I know the if statement is still wrong but im not sure what I need to change to fix it?

Can anyone help me with this?

ok I have an idea

for(int q = 0; q < rolls.length; q++)
			{	
				if(rolls[q] > most[q])
					rolls[q] = most[q];
				if(rolls[q] < fewest[q])
					rolls[q] = fewest[q];
					sum[q]++;
			}

Any better?

Close - but you have the assignment statements going backwards. Remember that the value will be copied from the variable on the right into the one on the left. The left hand variable is called an "lvalue" and must represent a memory location. So, for example, the statement 5 = x; will produce an error because 5 is a numeric constant and doesn't represent a location in memory. On the other hand, x = 5; will take the value 5 and store it in the variable x, assuming x is a numeric data type.

Finally, the statement sum[q++]; is wrong. You want to take the value in the rolls array and add it to the value in the sum array.

I will be away from the computer for most of the day today. Happy Thanksgiving! I'll try to remember to check back in later tonight. Good luck.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.