Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Random;
- public class DiceHelper {
- private static final Random rng = new Random();
- public static void main(String[] args) {
- }
- /**
- * Returns the result of rolling a dice with s sides.
- *
- * @param sides the number of sides on the dice
- * @return the sum of dice rolled
- */
- public static int roll(int sides) {
- return rng.nextInt(sides) + 1;
- }
- /**
- * Returns the sum of rolling n dice with s sides.
- *
- * @param sides the number of sides on each dice to roll
- * @param numRolls the number of dice to roll
- * @return the sum of dice rolled
- */
- public static int roll(int sides, int numRolls) {
- int sum = 0;
- for(int i = 0; i < numRolls; i++) {
- sum += roll(sides);
- }
- return sum;
- }
- /**
- * Returns the sum of rolling n dice with s sides
- * and adds m to the result of each roll.
- *
- * @param sides the number of sides on each dice to roll
- * @param numRolls the number of dice to roll
- * @param modifier the amount to add to the result of each dice roll
- * @return the sum of dice rolled
- */
- public static int roll(int sides, int numRolls, int modifier) {
- int sum = 0;
- for(int i = 0; i < numRolls; i++) {
- sum += roll(sides) + modifier;
- }
- return sum;
- // equivalent:
- // return roll(sides, numRolls) + numRolls * modifier);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement