Advertisement
Guest User

Untitled

a guest
Apr 24th, 2015
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.34 KB | None | 0 0
  1. import java.util.Random;
  2.  
  3. public class DiceHelper {
  4.    
  5.     private static final Random rng = new Random();
  6.    
  7.     public static void main(String[] args) {
  8.        
  9.     }
  10.    
  11.     /**
  12.      * Returns the result of rolling a dice with s sides.
  13.      *
  14.      * @param sides     the number of sides on the dice
  15.      * @return          the sum of dice rolled
  16.      */
  17.     public static int roll(int sides) {
  18.         return rng.nextInt(sides) + 1;
  19.     }
  20.    
  21.     /**
  22.      * Returns the sum of rolling n dice with s sides.
  23.      *
  24.      * @param sides     the number of sides on each dice to roll
  25.      * @param numRolls  the number of dice to roll
  26.      * @return          the sum of dice rolled
  27.      */
  28.     public static int roll(int sides, int numRolls) {
  29.         int sum = 0;
  30.         for(int i = 0; i < numRolls; i++) {
  31.             sum += roll(sides);
  32.         }
  33.         return sum;
  34.     }
  35.    
  36.     /**
  37.      * Returns the sum of rolling n dice with s sides
  38.      * and adds m to the result of each roll.
  39.      *
  40.      * @param sides     the number of sides on each dice to roll
  41.      * @param numRolls  the number of dice to roll
  42.      * @param modifier  the amount to add to the result of each dice roll
  43.      * @return          the sum of dice rolled
  44.      */
  45.     public static int roll(int sides, int numRolls, int modifier) {
  46.         int sum = 0;
  47.         for(int i = 0; i < numRolls; i++) {
  48.             sum += roll(sides) + modifier;
  49.         }
  50.         return sum;
  51.         // equivalent:
  52.         // return roll(sides, numRolls) + numRolls * modifier);
  53.     }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement