# My Dice Simulator

Page 1 of 1

## 2 Replies - 4637 Views - Last Post: 04 January 2011 - 01:28 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=208302&amp;s=b71afc6ab8e5bab46e67f71c01aa950f&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 isaiah1994

Reputation: 0
• Posts: 4
• Joined: 03-November 10

# My Dice Simulator

Posted 04 January 2011 - 12:58 PM

Hello guys, im going back to an old dice simulator i made awhile ago and im going to edit it by adding another die. Im a bit rusty and i was wondering if someone can help get started again on where i should add another die
```/*
Die.java
Isaiah Antoine
Ver 2.4 Date 12/7/10
Rolls a die
*/

import javax.swing.*;

class Die
{
public int roll()
{
int rollNum = 0;
double randNum = (int) (Math.random() * 6);
rollNum = (int) (randNum + 1);

return(rollNum);
}

//***********************************
public void oneDie()
{
String numRolls = JOptionPane.showInputDialog("How many times would you want to roll the die big boy? ;)/>");

int num = Integer.parseInt(numRolls);

int count = 0;
int total = 0;
int amount1 = 0;
int amount2 = 0;
int amount3 = 0;
int amount4 = 0;
int amount5 = 0;
int amount6 = 0;
//the number integers
for(count = 1; count <= num; count++)// the die itself

{
Die cube = new Die();//the die rolls count :)/>
int value1 = cube.roll();
total += value1;
switch(value1)
{
case 1:
amount1++;
break;
case 2:
amount2++;
break;
case 3:
amount3++;
break;
case 4:
amount4++;
break;
case 5:
amount5++;
break;
case 6:
amount6++;
break;
}

}

double average = total / count;
String average1 = "The average for " + (count - 1) + " rolls is a " + average;
String numbers = "\n# of 1's: " + amount1 + "\n# of 2's: " + amount2 + "\n# of 3's: " + amount3 + "\n# of 4's: " + amount4 + "\n# of 5's: " + amount5 + "\n# of 6's: " + amount6;

JOptionPane.showMessageDialog(null, average1 + numbers);
}

public void twoDie()// the loop for the second run
{
String numRolls = JOptionPane.showInputDialog("How many times do you want to roll the die big boy ;)/>");
int num = Integer.parseInt(numRolls);
Die cube = new Die();
Die cube2 = new Die();

int count = 0;
int total = 0;
int amount2 = 0;
int amount3 = 0;
int amount4 = 0;
int amount5 = 0;
int amount6 = 0;
int amount7 = 0;
int amount8 = 0;
int amount9 = 0;
int amount10 = 0;
int amount11 = 0;
int amount12 = 0;

for(count = 1; count <= num; count++)
{

int value1 = cube.roll();
int value2 = cube2.roll();

int value = value1 + value2;

switch(value)
{
case 2:
amount2++;
break;
case 3:
amount3++;
break;
case 4:
amount4++;
break;
case 5:
amount5++;
break;
case 6:
amount6++;
break;
case 7:
amount7++;
break;
case 8:
amount8++;
break;
case 9:
amount9++;
break;
case 10:
amount10++;
break;
case 11:
amount11++;
break;
case 12:
amount12++;
break;
}

total += value;
}

double average = total / count;

String average1 = "The average for " + (count - 1) + " rolls is a " + average;

String numbers1 = "\n# of 2's: " + amount2 + "\n# of 3's: " + amount3 + "\n# of 4's: " + amount4 + "\n# of 5's: " + amount5 + "\n# of 6's: " + amount6;
String numbers2 = "\n# of 7's: " + amount7 + "\n# of 8's: " + amount8 + "\n# of 9's: " + amount9 + "\n# of 10's: " + amount10 + "\n# of 11's: " + amount11 + "\n# of 12's: " + amount12;

JOptionPane.showMessageDialog(null, average1 + numbers1 + numbers2);
}

}

```

Is This A Good Question/Topic? 0

## Replies To: My Dice Simulator

### #2 g00se

• D.I.C Lover

Reputation: 3057
• Posts: 13,119
• Joined: 20-September 08

## Re: My Dice Simulator

Posted 04 January 2011 - 01:12 PM

You don't need to create any more than one instance of Die, which you do inside a loop. You don't even need to do that in order to simulate rolling multiple dice. All you need do is fill in the following method:

```public int[] roll(int numSimultaneous) {
...
}

```

This post has been edited by g00se: 04 January 2011 - 01:13 PM

### #3 pbl

• There is nothing you can't do with a JTable

Reputation: 8363
• Posts: 31,955
• Joined: 06-March 08

## Re: My Dice Simulator

Posted 04 January 2011 - 01:28 PM

A lot of code for nothing and you should learn how to use array
```import java.util.Random;

public class Dice {

// constructor receives number of dice and number of roll
Dice(int nbDices, int nbRoll) {
Random ran = new Random();
// store number return
int[] total = new int[nbDices * 6 + 1];

for(int i = 0; i < nbRoll; ++i) {
// total for nbDices
int val = 0;
for(int j = 0; j < nbDices; ++j) {
val += ran.nextInt(6) + 1;
}
// cumul total
++total[val];
}
// display result
System.out.println("\n" + nbDices + " dices " + nbRoll + " rolls:");
for(int i = nbDices; i < total.length; ++i) {
System.out.print(" [" + i + "]=" + total[i]);
}
System.out.println();
}

public static void main(String[] args) {
new Dice(1, 10);
new Dice(2, 10);
new Dice(2, 5);
new Dice(3, 10);
}
}

```