# Dice and Random Number Generator

Page 1 of 1

## 7 Replies - 8669 Views - Last Post: 24 March 2011 - 06:37 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=224049&amp;s=2051eee5bf4f3514908dca6d56993989&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 patelrb4

Reputation: 0
• Posts: 4
• Joined: 24-March 11

# Dice and Random Number Generator

Posted 24 March 2011 - 07:56 AM

How do I write a program which reads in one value called Top, the number of Random samples to test. Using the Die class discussed in the class,create "Top" number of rolls of dice. The program should calculate and display the number of times the simulated roll of dice was a 2, the number of times it was a 7, and the number of times it was a 12. Also display the average roll with exactly 3 decimal places.

I was able to write the Die class but I could not create "Top" number of rolls of dice. Top is the user input.

Here is the Die class

```//  Die.java
//  Ravibhai Patel
//  Represents one die (singular of dice) with faces showing values
//  between 1 and 6.

public class Die
{
private final int MAX = 6;  // maximum face value

private int faceValue;  // current value showing on the die

//  Constructor: Sets the initial face value.

public Die()
{
faceValue = 1;
}

//  Rolls the die and returns the result.

public int roll()
{
faceValue = (int)(Math.random() * MAX) + 1;

return faceValue;
}

//  Face value mutator.

public void setFaceValue (int value)
{
faceValue = value;
}

//  Face value accessor.

public int getFaceValue()
{
return faceValue;
}

//  Returns a string representation of this die.

public String toString()
{
String result = Integer.toString(faceValue);

return result;
}
}

```

This post has been edited by macosxnerd101: 24 March 2011 - 11:08 AM
Reason for edit:: Please use code tags

Is This A Good Question/Topic? 0

## Replies To: Dice and Random Number Generator

### #2 TFoSSDQ

• D.I.C Regular

Reputation: 123
• Posts: 253
• Joined: 09-December 10

## Re: Dice and Random Number Generator

Posted 24 March 2011 - 08:20 AM

Well obviously you would get Top and initialize two of your Die class. Then you would want three variables to hold the number of occurances of 2, 7, and 12. A sum variable to hold all of them would be good too. In a for loop then:
```for(int i = 0; i < Top; i++)
{
int d1 = die1.roll();
int d2 = die2.roll();
int total = d1+d2;
if(total == 2)
2Total++;
else if(total == 7)
7Total++;
else if(total == 12)
12Total++;
sum += total;
}

```

Then later:
```double average = ((int)(((double)sum/Top)*1000))/1000.0; //complex for three decimal places max

```

### #3 GregBrannon

• D.I.C Lover

Reputation: 2250
• Posts: 5,340
• Joined: 10-September 10

## Re: Dice and Random Number Generator

Posted 24 March 2011 - 11:00 AM

Are you asking how to get user input? There are two ways: 1) Ask the user for input and read it from the keyboard, or 2) Read the input as a command line argument when the program is run.

I think you're more interested in the first, and to accomplish that look at the Java Scanner class. You can find examples of how to use Scanner within the threads here, there may be tutorials on getting user input, or just Google for it.

### #4 patelrb4

Reputation: 0
• Posts: 4
• Joined: 24-March 11

## Re: Dice and Random Number Generator

Posted 24 March 2011 - 01:26 PM

Thanks you guys for the replies. However, I am a little bit confused
This is how the out put should look like

Enter the number of rolls : 5

Die One: 5, Die Two: 2 Sum 7
Average 3.500

Die One: 3, Die Two: 1 Sum 4
Average 2.000

Die One: 6, Die Two: 2 Sum 8
Average 4.000

Die One: 1, Die Two: 5 Sum 6
Average 3.000

Die One: 5, Die Two: 4 Sum 9
Average 4.5000

Now I need to find out of total times, how many times did 2,7, and 12 come out?
There is where I am confused. I don't know how to do that.

Here is the Public Class Dice (including Main Method)

//Ravibhai Patel
//CMSC 255
// Dice Random Number Generator

import java.util.Scanner;
import java.text.DecimalFormat;

public class Dice
{
public static void main (String [] args)
{

int top = 0;

Scanner input = new Scanner(System.in);
System.out.print("Number of rolls of the Dice :");
top = input.nextInt();

int sum;

Die die1 = new Die();
Die die2 = new Die();

do{

int diee1 = die1.roll();
int diee2 = die2.roll();
System.out.println ("Die One: " + diee1 + ", Die Two: " + diee2);

double average = (diee1+diee2)/2.0;
DecimalFormat fmt = new DecimalFormat("0.000"); //Creates an object of NumberFormat class
String ravi = fmt.format(average);

System.out.println ("Average : " + ravi);
System.out.println();

top--;

}while (top >= 1);

}
}

### #5 GregBrannon

• D.I.C Lover

Reputation: 2250
• Posts: 5,340
• Joined: 10-September 10

## Re: Dice and Random Number Generator

Posted 24 March 2011 - 01:41 PM

You could use a 2, 7, and 12 counter and increment the counter each time 2, 7, and 12 are rolled.

E.g.:

```int countTwo;
int countSeven;
int countTwelve;

if ( total == 2 )
{
countTwo++;
}
```

Etc.

### #6 TFoSSDQ

• D.I.C Regular

Reputation: 123
• Posts: 253
• Joined: 09-December 10

## Re: Dice and Random Number Generator

Posted 24 March 2011 - 01:42 PM

It's a small modification on what I did before then:
```DecimalFormat format = new DecimalFormat("0.000");
for(int i = 0; i < Top; i++)
{
int d1 = die1.roll();
int d2 = die2.roll();
int total = d1+d2;
if(total == 2)
2Total++;
else if(total == 7)
7Total++;
else if(total == 12)
12Total++;
System.out.println("Die One: "+d1+", Die Two: "+d2+" Sum "+total;
double average = total/2.0;
System.out.println("Average "+format.format(average));
System.out.println();
}

```

Same thing with three counters for 2, 7, 12.
later you would want to:
```System.out.println("Number of 2s: "+2Total);
System.out.println("Number of 7s: "+7Total);
System.out.println("Number of 12s: "+12Total);

```

### #7 patelrb4

Reputation: 0
• Posts: 4
• Joined: 24-March 11

## Re: Dice and Random Number Generator

Posted 24 March 2011 - 04:24 PM

Thanks GregBrannon and TFoSSDQ

Your reviews very helpful. Both of you helped me with great knowledge.

```//Ravibhai Patel
//CMSC 255
// Dice Random Number Generator

import java.util.Scanner;			// Imports Scanner Class
import java.text.DecimalFormat;			// Imports DecimalFormat class

public class Dice
{
public static void main (String [] args)		//Main Method
{

int top = 0;

// Get user imput, the number times to roll the dice

Scanner input = new Scanner(System.in);
System.out.print("Number of rolls of the Dice :");
top = input.nextInt();
System.out.println();

int sum, sum2=0, sum7=0, sum12=0;

Die die1 = new Die();     // Creates object of Die class
Die die2 = new Die();		// it also sets the initial values, MAX-6, and Lowest-1, for cubic dice

do{           // Rolls the dice,"top" times

int diee1 = die1.roll();
int diee2 = die2.roll();
System.out.println ("Die One: " + diee1 + ", Die Two: " + diee2);

double average = (diee1+diee2)/2.0;			// Calculate the average
DecimalFormat fmt = new DecimalFormat("0.000");         //Creates an object of NumberFormat class
String ravi = fmt.format(average);
System.out.println ("Average : " + ravi);

sum=(diee1+diee2);				// Calculate Sum
System.out.println("Sum : " + sum);
System.out.println();

top--;

if(sum == 2)			// counts if sum is 2,7, or 12
sum2++;
if(sum == 7)
sum7++;
if(sum ==12)
sum12++;

}while (top >= 1);

//Prints the values

System.out.println("Number of 2s: "+sum2);
System.out.println("Number of 7s: "+sum7);
System.out.println("Number of 12s: "+sum12);

}
}

```

### #8 TFoSSDQ

• D.I.C Regular

Reputation: 123
• Posts: 253
• Joined: 09-December 10

## Re: Dice and Random Number Generator

Posted 24 March 2011 - 06:37 PM

Looks all good