# Generating Standard Uniform random variable

The book I am following has a problem that states:

Let $$U$$ be a Standard Uniform random variable. Show all the steps required to generate

Then proceeds to list off questions on generating other random variables. In the solution I am following, it states "Given, $$U\sim(0,1)$$ and $$U=0.3972$$".

Where are they getting these "givens" from?

Edit, the full question is:

Let U be a Standard Uniform random variable. Show all the steps required to generate a) an Exponential random variable with the probability λ = 3.0;

• Is that the entirety of the solution? If it says more than that, please post the full solution, as we can't really determine what is being said from that fragment alone. And of course post the corresponding question that goes with this solution. – kccu Apr 9 at 19:12

## 2 Answers

Many statistical software programs use the 'Mersenne Twister' pseudorandom number generator. See this link for some information on this generator (the default in R) and other well vetted generators. More particularly, see Wikipedia on the Mersenne-Twister.

(There are no set rules for making a pseudorandom number generator, using congruential generators or otherwise. We know lots of things not to do, but no sure rules for success. Thus generators are tested using 'batteries' of problems that have proved difficult to simulate. A well-vetted generator is one that has passed many such tests.)

In R statistical software, the function runif samples the indicated number of observations from a uniform distribution. Thus the following R code samples $$m = 10,000$$ observations from $$\mathsf{Exp}(\lambda = 3)$$ according to the inverse CDF transformation shown by @peter 5 (+1).

set/seed(416)  # for reproducibility
m = 10^4;  u = runif(m)
x = -(1/3)*log(u)


Histograms of the vectors u and x, each containing $$m$$ elements, are shown below, along with the respective density functions.

par(mfrow=c(1,2))
hist(u, prob=T, col="skyblue2", main="Uniform Sample with Density of UNIF(0,1)")
curve(dunif(x), add=T, n=10001, lwd=2, col="red")
hist(x, prob=T, col="skyblue2", main="Exponential Sample with Density of EXP(rate=3)")
curve(dexp(x, 3), add=T, n=10001, lwd=2, col="red")
par(mfrow=c(1,1)) Notice that the plots show good agreement of the histograms of samples with the density functions of the respective distributions.

More formally, here are results of Kolmogorov-Smirnov goodness-of-fit tests based on the first 5000 observations in each sample (the samplie-size limit allowed by the implementation of this test in R). P-values far above 5% indicate that samples are consistent with the claimed distributions.

ks.test(u[1:5000], "punif")

One-sample Kolmogorov-Smirnov test

data:  u[1:5000]
D = 0.013297, p-value = 0.3396
alternative hypothesis: two-sided

ks.test(x[1:5000], "pexp", 3)

One-sample Kolmogorov-Smirnov test

data:  x[1:5000]
D = 0.013297, p-value = 0.3396
alternative hypothesis: two-sided


An idea is to use the Inverse Transform Method: Let $$F(x)$$, $$x\in\mathbb{R}$$, denote any cumulative distribution function. Let $$F^{-1}(y)$$, $$y\in[0,1]$$ denote the inverse function and define $$X = F^{-1}(U)$$, where $$U\sim U(0,1)$$. Then, $$P(X ≤ x) = F(x)$$.

As a result, to simulate from an exponential with parameter $$\lambda$$:

Step 1. Generate $$u\sim U(0,1)$$.

Step 2. Set $$u=1-e^{-\lambda x}$$ (CDF of Exponential).

Soving for x, yields: $$x = -\frac{1}{\lambda}\log(u)$$. This random variable comes from exponential distribution with parameter $$\lambda$$.

In order to generate a random variable from $$U(0,1)$$, there are two possible ways: (1) The standard-immediate way; and (2) The technicaly involved way. According to (1) you just call a random number generator, eg., in matlab it is the function rand, in R it is the function runif, etc. One way to implement (2), is to resort to the Multiplicative Congruential Method (for more information, see this https://wiki.math.uwaterloo.ca/statwiki/index.php?title=generating_Random_Numbers).

An algorithm to implement this method is the following. In what follows $$x_{0},\alpha, m \in \mathbb{N}$$.

Step 1. Start with a seed value $$x_{0}$$

Step 2. Apply the $$x_{i} = \alpha x_{i-1}$$ mod $$m$$, for $$i=1,2,\dots,n$$

Step 3. Set $$u_{i}=\displaystyle\frac{x_{i}}{m}$$, for $$i=1,2,\dots,n$$

Then, for a very large number $$n$$, $$u_{i}$$ maybe considered as $$U(0,1).$$ Of course, there are several rules on choosing the parameters $$x_{0},\alpha, m$$. For example, $$m$$ has to be a very large prime number. A "good" choice is $$\alpha=7^5-1$$ and $$m=2^{32}-1$$. For more information on this matter, look here https://en.wikipedia.org/wiki/Linear_congruential_generator#m_a_power_of_2,c=_0

• I am confused by the Generate u – Scott Glascott Apr 9 at 21:43
• @ScottGlascott, I updated my answer – peter5 Apr 10 at 4:07