1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.rng.examples.jpms.lib;
19
20 import org.apache.commons.rng.UniformRandomProvider;
21 import org.apache.commons.rng.sampling.distribution.ContinuousSampler;
22 import org.apache.commons.rng.sampling.distribution.GaussianSampler;
23 import org.apache.commons.rng.sampling.distribution.ZigguratNormalizedGaussianSampler;
24
25
26
27
28 public class DiceGame {
29
30 private final UniformRandomProvider rng;
31
32 private final ContinuousSampler sampler;
33
34 private final int rounds;
35
36 private final int players;
37
38
39
40
41
42
43
44
45 public DiceGame(int players,
46 int rounds,
47 UniformRandomProvider rng,
48 double mu,
49 double sigma) {
50 this.rng = rng;
51 this.rounds = rounds;
52 this.players = players;
53 sampler = GaussianSampler.of(ZigguratNormalizedGaussianSampler.of(rng), mu, sigma);
54 }
55
56
57
58
59
60
61 public int[] play() {
62 final int[] scores = new int[players];
63
64 for (int i = 0; i < rounds; i++) {
65 doRound(scores);
66 }
67 return scores;
68 }
69
70
71
72
73
74
75 private void doRound(int[] currentScores) {
76 for (int i = 0; i < players; i++) {
77 currentScores[i] += roll();
78 }
79 }
80
81
82
83
84 private int roll() {
85 int score = 0;
86 final int n = numberOfDice();
87 for (int i = 0; i < n; i++) {
88 score += singleRoll();
89 }
90 return score;
91 }
92
93
94
95
96 private int singleRoll() {
97 return rng.nextInt(6);
98 }
99
100
101
102
103 private int numberOfDice() {
104 final double n = Math.round(sampler.sample());
105 return n <= 0 ? 0 : (int) n;
106 }
107 }