Problem Statement
Our goal is to solve a generalized version of this problem in which we roll nDice identical dice, each with sides labelled 1,2,...,maxSide. We want to know the probability that the sum of the dice is greater than or equal to theSum given that at least one of the dice shows the value v. Create a class Conditional that contains a method probability that is given nDice,maxSide, v, and theSum and that returns the desired conditional probability.
Definition
- Class:
- Conditional
- Method:
- probability
- Parameters:
- int, int, int, int
- Returns:
- double
- Method signature:
- double probability(int nDice, int maxSide, int v, int theSum)
- (be sure your method is public)
Notes
- The returned value must be accurate to within a relative or absolute value of 1E-9.
Constraints
- nDice and maxSide will be between 1 and 50, inclusive.
- v will be between 1 and maxSide, inclusive.
- theSum will be between 1 and maxSide*nDice, inclusive.
Examples
2
6
6
12
Returns: 0.09090909090909091
This is the example above whose answer is 1/11. (Of course, the sum cannot be greater than 12.)2
6
6
6
Returns: 1.0
Given that at least one of the dice shows a 6 the sum of the 2 dice must be at least 7.1
9
3
3
Returns: 1.0
2
3
2
4
Returns: 0.6
Two 3-sided (!) dice with at least one 2 showing: 12 22 32 21 23 are the 5 possible equally likely results, and 3 of the 5 have a sum greater than or equal to 4.