Runoob Tutorial ArchiveEnglish

Discrete Random Variables — From Rolling Dice to Probability Mass Function

Discrete Random Variables -- From Rolling Dice to Probability Mass Function

Random variables formalize "random events" into a mathematical tool. Discrete random variables have a finite or countable set of values.


Concept Analysis

Random Variable

A random variable is a variable whose value is uncertain, and each value has a corresponding probability.

PMF (Probability Mass Function)

[ P(X = x_k) = p_k, \quad \sum_k p_k = 1 ]

Bernoulli distribution

A single trial with two outcomes: P(success)=p, P(failure)=1-p. Mathematical modeling of binary classification problems.

Binomial distribution

[ P(X=k) = \binom{n}{k} p^k (1-p)^{n-k} ]

The probability of getting k successes in n independent Bernoulli trials.


Real-life examples

Rolling a die: X is the number shown, P(X=1)=1/6, P(X=2)=1/6, ...

Tossing a coin 10 times, the number of heads X follows a Binomial distribution Binomial(10, 0.5), the most likely outcome is 5.


Python Hands-on Practice

Example

import

numpy

as

np

Dice PMF

rolls

=

np.

random

.

randint

(

1

,

7

,

size

=

10000

)

for

v

in

range

(

1

,

7

)

:

   

print

(

f

"Score {v}: {np.mean(rolls==v):.4f}"

)

Bernoulli

print

(

f

"\nBernoulli p=0.3, 1000 trials, success rate={np.random.binomial(1,0.3,1000).mean():.3f}"

)

Binomial distribution Binomial(10, 0.3)

samples

=

np.

random

.

binomial

(

10

,

0.3

,

10000

)

k_best

=

np.

bincount

(

samples

)

.

argmax

(

)

print

(

f

Binomial distribution n=10, p=0.3: Most likely k={k_best} (theoretical=np=3)

)

Run output:

Original prompt (Chinese, left untranslated)



点数1: 0.1628

点数2: 0.1661

点数3: 0.1686

点数4: 0.1686

点数5: 0.1637

点数6: 0.1702



伯努利 p=0.3, 1000次试验, 成功率=0.295

二项分布 n=10,p=0.3: 最可能 k=3 (理论=np=3)

Binomial distribution PMF interactive chart

Probability Mass Function (PMF) of Binomial(n=10, p=0.3). Adjust the slider to observe the distribution shape under different p values:


Application scenarios in AI

Binary classification label = Bernoulli distribution

Logistic regression and binary classifiers model the label y ∈ {0,1} as a Bernoulli distribution: P(y=1|x) = \sigma(wx+b), P(y=0|x) = 1-\sigma(wx+b). Binary cross-entropy loss is the negative log-likelihood under this assumption. This is why the output layer for binary classification tasks uses Sigmoid + BCE.

Multi-class classification = Category distribution (a single trial of a multinomial distribution)

In multi-class classification, the label is a one-hot vector, and the sum of probabilities for each class output by Softmax is 1. This is equivalent to a single n-sided "dice" roll where each side has a different probability. Cross-entropy loss under this assumption equals the negative log-likelihood.

Dropout regularization = Bernoulli trial

During training, each neuron is kept with probability p (Bernoulli trial) and dropped with probability 1-p. This is equivalent to training a different subnetwork in each iteration, achieving the effect of ensemble learning. During testing, all neurons are activated, but the output is multiplied by p for scaling compensation.

Exploration in reinforcement learning = Sampling from a discrete distribution

DQN often uses an epsilon-greedy strategy: randomly select an action with probability ε (Bernoulli), and select the optimal action with probability 1-ε. This is the simplest exploration strategy, ensuring the agent does not get stuck in a suboptimal policy forever.