Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Jan 13th, 2013  |  syntax: None  |  size: 1.28 KB  |  views: 17  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. public class HowLongWillMyGameLastSlightlyBetter {
  2.        
  3.         public static double probFinishOnTurnNOrLaterRecursive(int turnStop,
  4.                         int specialCards, int normalCards) throws IllegalArgumentException {
  5.                 if (turnStop > specialCards + normalCards || turnStop < specialCards) {
  6.                         throw new IllegalArgumentException(
  7.                                         "That's just a silly number to ask for since it's out of range.");
  8.                 }
  9.                 double tempProbability = (double) (specialCards)
  10.                                 / (double) (specialCards + normalCards);
  11.                
  12.                 if (turnStop == specialCards + normalCards) {
  13.                         return tempProbability;
  14.                 }
  15.                 return (tempProbability + probFinishOnTurnNOrLaterRecursive(turnStop,
  16.                                 specialCards, normalCards - 1) * (1.0 - tempProbability));
  17.         }
  18.  
  19.         public static void main(String[] args) {
  20.                 int specialCards = 5;
  21.                 int normalCards = 16;
  22.                 System.out.println("With " + specialCards + " special cards and "
  23.                                 + normalCards
  24.                                 + " normal cards in the deck the following is true: ");
  25.                 for (int i = specialCards + normalCards; i >= specialCards; i--) {
  26.                         double cumulativeProbability = probFinishOnTurnNOrLaterRecursive(i,
  27.                                         specialCards, normalCards);
  28.                         System.out.println("The probability to finish on turn " + i
  29.                                         + " or later is "
  30.                                         + (((int) (cumulativeProbability * 10000)) / 100.0) + "%");
  31.                 }
  32.  
  33.         }
  34.  
  35.  
  36.        
  37. }