Skip to content

Instantly share code, notes, and snippets.

@thosakwe
Created April 24, 2016 21:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thosakwe/9f0d74d79819333359a49c3effc9f026 to your computer and use it in GitHub Desktop.
Save thosakwe/9f0d74d79819333359a49c3effc9f026 to your computer and use it in GitHub Desktop.
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.Scanner;
public class DiceProbability {
private static Random random = new Random();
private static int roll(int sides) {
int first = random.nextInt(sides) + 1;
int second = random.nextInt(sides) + 1;
return first + second;
}
public static void main(String[] args) {
int rolls1 = 0;
int rolls2 = 0;
int rolls3 = 0;
int rolls4 = 0;
int rolls5 = 0;
int rolls6 = 0;
int rolls7 = 0;
int rolls8 = 0;
int rolls9 = 0;
int rolls10 = 0;
int rolls11 = 0;
int rolls12 = 0;
int rolls13 = 0;
int rolls14 = 0;
int rolls15 = 0;
int rolls16 = 0;
int rolls17 = 0;
int rolls18 = 0;
int rolls19 = 0;
int rolls20 = 0;
int rolls21 = 0;
int rolls22 = 0;
Scanner scanner = new Scanner(System.in);
System.out.print("Times to roll: ");
int times = scanner.nextInt();
for (int i = 0; i < times; i++) {
int roll = roll(11);
switch (roll) {
case 1:
rolls1++;
break;
case 2:
rolls2++;
break;
case 3:
rolls3++;
break;
case 4:
rolls4++;
break;
case 5:
rolls5++;
break;
case 6:
rolls6++;
break;
case 7:
rolls7++;
break;
case 8:
rolls8++;
break;
case 9:
rolls9++;
break;
case 10:
rolls10++;
break;
case 11:
rolls11++;
break;
case 12:
rolls12++;
break;
case 13:
rolls13++;
break;
case 14:
rolls14++;
break;
case 15:
rolls15++;
break;
case 16:
rolls16++;
break;
case 17:
rolls17++;
break;
case 18:
rolls18++;
break;
case 19:
rolls19++;
break;
case 20:
rolls20++;
break;
case 21:
rolls21++;
break;
case 22:
rolls22++;
break;
}
}
System.out.println("Sum of Dice Probability");
System.out.println(" 1s: " + ((rolls1 / (double) times) * 100.0));
System.out.println(" 2s: " + ((rolls2 / (double) times) * 100.0));
System.out.println(" 3s: " + ((rolls3 / (double) times) * 100.0));
System.out.println(" 4s: " + ((rolls4 / (double) times) * 100.0));
System.out.println(" 5s: " + ((rolls5 / (double) times) * 100.0));
System.out.println(" 6s: " + ((rolls6 / (double) times) * 100.0));
System.out.println(" 7s: " + ((rolls7 / (double) times) * 100.0));
System.out.println(" 8s: " + ((rolls8 / (double) times) * 100.0));
System.out.println(" 9s: " + ((rolls9 / (double) times) * 100.0));
System.out.println("10s: " + ((rolls10 / (double) times) * 100.0));
System.out.println("11s: " + ((rolls11 / (double) times) * 100.0));
System.out.println("12s: " + ((rolls12 / (double) times) * 100.0));
System.out.println("13s: " + ((rolls13 / (double) times) * 100.0));
System.out.println("14s: " + ((rolls14 / (double) times) * 100.0));
System.out.println("15s: " + ((rolls15 / (double) times) * 100.0));
System.out.println("16s: " + ((rolls16 / (double) times) * 100.0));
System.out.println("17s: " + ((rolls17 / (double) times) * 100.0));
System.out.println("18s: " + ((rolls18 / (double) times) * 100.0));
System.out.println("19s: " + ((rolls19 / (double) times) * 100.0));
System.out.println("20s: " + ((rolls20 / (double) times) * 100.0));
System.out.println("21s: " + ((rolls21 / (double) times) * 100.0));
System.out.println("22s: " + ((rolls22 / (double) times) * 100.0));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment