TopcoderARCHIVE
Archive/Problems/DieRollingGame
Algorithm · Problem 17430

DieRollingGame

Problem statement, definition, constraints, and public examples.

Problem Statement

You are playing a game in which you roll a single die, with faces numbered 1..n. With each roll, you score points equal to the value shown on the die. You keep rolling until you either reach total, and win, or else exceed total, and lose the game.

Assuming you are using a fair die that has an equal chance of landing on any side, what is the probability that you win the game?

Definition

Class:
DieRollingGame
Method:
winChance
Parameters:
int, int
Returns:
double
Method signature:
double winChance(int n, int total)
(be sure your method is public)

Constraints

  • n will be between 2 and 20.
  • total will be between 1 and 100.

Examples

  1. 6
    1
    Returns: 0.16666666666666666
    You're only going to make a single roll here, and you have a 1/6 chance of getting the 1.
  2. 4
    2
    Returns: 0.3125
    Here, it's a little more interesting. You have a 1/4 chance of winning on the first roll, with a 2, as well as a 1/4 * 1/4 chance of winning with two rolls of 1.
  3. 2
    3
    Returns: 0.625
    Since we're basically flipping a coin, we have an equal chance of rolling a 1 or a 2, leaving 1 or 2 more points that we need to win. We have a 1/2 chance to get 1 point, or a 3/4 chance to successfully score 2 points. 1/2 * 1/2 + 1/2 * 3/4 = 5/8
  4. 20
    20
    Returns: 0.12634750976878192
    We could win this in a single roll of 20, 20 consecutive rolls of 1, or a whole lot of other possibilities in between.
← All problems