Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // By: Sammy Samkough
- // 7s & 11s
- // Counts number of times you roll a sum of 7 or 11
- import java.util.Random;
- public class SevenEleven
- {
- private static Random r = new Random();
- private int numFaces;
- private int faceValue;
- public SevenEleven()
- {
- numFaces = 6;
- faceValue = 1;
- }
- public SevenEleven(int sides)
- {
- numFaces = sides;
- faceValue = 1;
- }
- public void roll()
- {
- faceValue = (int)(Math.random() * numFaces) + 1;
- }
- // get results of rolled die
- public int getFaceValue()
- {
- return faceValue;
- }
- public String toString()
- {
- // we put strings since faceValue is an int
- return "" + faceValue;
- }
- }
- -------------------------------------------------------------------------------------------------------------------------------
- // By: Sammy Samkough
- // 7s & 11s
- // Counts number of times you roll a sum of 7 or 11
- public class SevenElevenMain
- {
- public static void main(String args[])
- {
- // create
- SevenEleven d1 = new SevenEleven();
- SevenEleven d2 = new SevenEleven(6);
- int sum = 0;
- int x = 0, y = 0;
- // rolls 5000 times
- for (int i = 0; i < 5000; i++)
- {
- // rolls
- d1.roll();
- d2.roll();
- // get results
- d1.getFaceValue();
- d2.getFaceValue();
- sum = d1.getFaceValue() + d2.getFaceValue();
- if (sum == 7)
- {
- x++;
- }
- if (sum == 11)
- {
- y++;
- }
- }
- // print out
- System.out.println("D1 = " + d1);
- System.out.println("D2 = " + d2);
- System.out.println("Sum = " + sum);
- System.out.println("The expected rolls for 7 were 833 but it actually gets called " + x + " time(s).");
- System.out.println("The expected rolls for 11 were 278 but it actually gets called " + y + " time(s).");
- }
- }
- /*
- D1 = 6
- D2 = 5
- Sum = 11
- The expected rolls for 7 were 833 but it actually gets called 825 time(s).
- The expected rolls for 11 were 278 but it actually gets called 260 time(s).
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement