Loading content...
In probability theory and statistics, discrete uniform distributions are fundamental models that appear throughout machine learning, game theory, and simulation systems. A fair n-sided die represents one of the simplest examples of such a distribution, where each outcome from 1 to n is equally likely with probability 1/n.
Understanding the expected value (mean) and variance of random variables is essential for probabilistic reasoning in AI systems, risk assessment, and statistical inference.
Expected Value (Mean): The expected value represents the theoretical average outcome if the die were rolled infinitely many times. For a fair n-sided die with faces numbered 1 through n, the expected value is:
$$\mathbb{E}[X] = \frac{1 + 2 + 3 + \cdots + n}{n} = \frac{n + 1}{2}$$
This can be derived from the arithmetic series sum formula: the sum of integers from 1 to n equals n(n+1)/2, divided by n outcomes.
Variance: Variance measures the spread or dispersion of the outcomes around the mean. It quantifies how much the die rolls deviate from the expected value on average. For a fair n-sided die, the variance is:
$$\text{Var}(X) = \mathbb{E}[X^2] - (\mathbb{E}[X])^2 = \frac{n^2 - 1}{12}$$
This formula can be derived using the sum of squares formula combined with the definition of variance.
Your Task: Write a Python function that computes both the expected value and variance for a fair n-sided die. The function should return these values as a tuple, with each value rounded to 4 decimal places.
n = 6(3.5, 2.9167)For a standard 6-sided die:
• Expected Value: (6 + 1) / 2 = 3.5 This means that over many rolls, the average outcome approaches 3.5.
• Variance: (6² - 1) / 12 = (36 - 1) / 12 = 35 / 12 ≈ 2.9167 This indicates moderate spread around the mean.
The function returns (3.5, 2.9167).
n = 4(2.5, 1.25)For a 4-sided die (like a tetrahedron):
• Expected Value: (4 + 1) / 2 = 2.5
• Variance: (4² - 1) / 12 = (16 - 1) / 12 = 15 / 12 = 1.25 A smaller variance reflects the narrower range of possible outcomes compared to a 6-sided die.
The function returns (2.5, 1.25).
n = 20(10.5, 33.25)For a 20-sided die (commonly used in tabletop RPGs):
• Expected Value: (20 + 1) / 2 = 10.5
• Variance: (20² - 1) / 12 = (400 - 1) / 12 = 399 / 12 = 33.25 The larger variance reflects the wider spread of outcomes possible with a 20-sided die.
The function returns (10.5, 33.25).
Constraints