-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDiceRolling.java
More file actions
51 lines (40 loc) · 1.04 KB
/
Copy pathDiceRolling.java
File metadata and controls
51 lines (40 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/**
* Simulate rolling two six-sided die, keep statistics
*
* @author Duke Software Team
* @version 1.0
*/
import java.util.Random;
public class DiceRolling
{
public void simulate(int rolls){
Random rand = new Random();
int [] counts = new int [13];
for(int k=0; k < rolls; k++){
int d1 = rand.nextInt(6) + 1; // returns a number 0 to 6
int d2 = rand.nextInt(6) + 1;
System.out.println("roll is " + d1 + "+" + d2 + "=" + (d1+d2));
counts[d1+d2] += 1;
}
for (int k=2; k <=12; k++) {
System.out.println(k + "'s=\t" + counts[k] + "\t" + 100.0 * counts[k]/rolls);
}
}
public void simpleSimulate(int rolls){
Random rand = new Random();
int twos = 0;
int twelves = 0;
for(int k=0; k < rolls; k++){
int d1 = rand.nextInt(6) + 1;
int d2 = rand.nextInt(6) + 1;
if (d1 + d2 == 2){
twos += 1;
}
else if (d1 + d2 == 12){
twelves += 1;
}
}
System.out.println("2's=\t" + twos + "\t" + 100.0 * twos/rolls);
System.out.println("12's=\t"+twelves+"\t"+100.0*twelves/rolls);
}
}