Third edition of Artificial Intelligence: foundations of computational agents, Cambridge University Press, 2023 is now available (including the full text).

#### 6.4.2.1 Sampling from a Single Variable

To generate samples from a single discrete or real-valued variable, *X*, first totally order the values in the
domain of *X*. For
discrete variables, if
there is no natural order, you can just create an arbitrary ordering.
Given this ordering, the **cumulative probability
distribution** is a function of *x*, defined by
*f(x)=P(X ≤ x)*.

To generate a random sample for *X*, select a random number *y* in the
range *[0,1]*. We select *y* from a uniform distribution to
ensure that each number between 0 and 1 has the same chance
of being chosen. Let *v* be the value of *X* that maps to *y* in
the cumulative probability distribution. That is,
*v* is the element of *dom(X)* such that *f(v)=y* or, equivalently, *v=f ^{-1}(y)*. Then,

*X=v*is a random sample of

*X*, chosen according to the distribution of

*X*.

**Example 6.22:**Consider a random variable

*X*with domain

*{v*. Suppose

_{1},v_{2},v_{3},v_{4}}*P(X=v*,

_{1})= 0.3*P(X=v*,

_{2})=0.4*P(X=v*, and

_{3})=0.1*P(X=v*. First, totally order the values, say

_{4})=0.2*v*. Figure 6.9 shows

_{1}< v_{2}< v_{3}< v_{4}*P(X)*, the distribution for

*X*, and

*f(X)*, the cumulative distribution for

*X*. Consider value

*v*; 0.3 of the range of

_{1}*f*maps back to

*v*. Thus, if a sample is uniformly selected from the

_{1}*Y*-axis,

*v*has a 0.3 chance of being selected,

_{1}*v*has a

_{2}*0.4*chance of being selected, and so forth.