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.