Statistics

Problem Statement for "WeightedDice"

Problem Statement

You have a weighted (unfair) six-sided die. You know the probability of it landing on any of the six sides. You are given these probabilities in double[] prob, in which the i-th element indicates the probability of rolling a value of i+1.

You are going to play a game in which you repeatedly roll the die until the total of your rolls equals or exceeds the target value. If you reach the target value exactly, you win. If you exceed the target value, you lose.

Return the probability of winning the game.

Definition

Class:
WeightedDice
Method:
winChance
Parameters:
double[], int
Returns:
double
Method signature:
double winChance(double[] prob, int target)
(be sure your method is public)

Constraints

  • prob will contain exactly 6 elmeents.
  • The elements of prob will sum to 1.0.
  • target will be between 1 and 100, inclusive.

Examples

  1. { 0.2, 0.2, 0.2, 0.2, 0.1, 0.1 }

    1

    Returns: 0.2

    Here with a target of 1, we must get a 1 on the first roll in order to win. This happens with a probability of 0.2.

  2. { 0.2, 0.2, 0.2, 0.2, 0.1, 0.1 }

    2

    Returns: 0.24000000000000002

    To score a 2, we can either roll a 2 on the first roll, with a probability 0.2, or else roll a 1, then another 1, with a probability 0.2 * 0.2 = 0.04. This gives us a total change of 0.2 + 0.04 = 0.24 of winning.

  3. { 1.0, 0.0, 0.0, 0.0, 0.0, 0.0 }

    5

    Returns: 1.0

    In this scenario, our die will always roll a 1, thus we are guaranteed to win for any target value.

  4. { 0.5, 0.5, 0.0, 0.0, 0.0, 0.0 }

    3

    Returns: 0.625

    We can get a total of 3 a few different ways: 1 + 2 (0.5 * 0.5 chance) 2 + 1 (0.5 * 0.5 chance) 1 + 1 + 1 (0.5 * 0.5 * 0.5 chance) 0.25 + 0.25 + 0.125 = 0.625

  5. { 0.5, 0.5, 0.0, 0.0, 0.0, 0.0 }

    100

    Returns: 0.6666666666666665

  6. {0.2, 0.2, 0.2, 0.2, 0.1, 0.1 }

    100

    Returns: 0.322580645161291

  7. {0.1, 0.12, 0.14, 0.16, 0.18, 0.3 }

    100

    Returns: 0.2439024391307422

  8. {0.17, 0.17, 0.16, 0.17, 0.16, 0.17 }

    100

    Returns: 0.2865329512893957


This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2024, TopCoder, Inc. All rights reserved.
This problem was used for: