Advertisement
Guest User

Untitled

a guest
Jan 13th, 2013
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  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. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement