View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * Example application.
27   */
28  public class DiceGame {
29      /** Underlying RNG. */
30      private final UniformRandomProvider rng;
31      /** Sampler. */
32      private final ContinuousSampler sampler;
33      /** Number of rounds in the game. */
34      private final int rounds;
35      /** Number of players in the game. */
36      private final int players;
37  
38      /**
39       * @param players Number of players.
40       * @param rounds Number of rounds.
41       * @param rng RNG.
42       * @param mu Mean.
43       * @param sigma Standard deviation.
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       * Play a game.
58       *
59       * @return the scores of all the players.
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       * Play a round and update the scores.
72       *
73       * @param currentScores Scores of the players.
74       */
75      private void doRound(int[] currentScores) {
76          for (int i = 0; i < players; i++) {
77              currentScores[i] += roll();
78          }
79      }
80  
81      /**
82       * @return the score of one round.
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       * @return a number between 1 and 6.
95       */
96      private int singleRoll() {
97          return rng.nextInt(6);
98      }
99  
100     /**
101      * @return the number of dice to roll.
102      */
103     private int numberOfDice() {
104         final double n = Math.round(sampler.sample());
105         return n <= 0 ? 0 : (int) n;
106     }
107 }