Random numbers

A random variable is a variable that takes a value with a certain probability. A random variable is always associated with a probability distribution.

An example is a random variable U[0, 1) that takes a real value between 0 and 1 EQUALLY. Meaning of “equally” is mathematically described as follows using p(x) as the probability density function of X = U[0, 1).

Prob[ x < X < x + dx] = p(x) dx

with sufficiently small interval dx.

A (pseudo) random number can be generated by a random number generator. “Mersenne twister” is a good algorith to generate U[0, 1) (C programing).

But in this lecture we use a Mathematica built-in command “Random” to generate various random numbers.

Random numbers uniformly distributed (Mathematica)

Random[]

Table[Random[],{10}]

data=Table[Random[],{100000}];

data

Histogram[data,{0,1,0.05}]

Histogram[data,{0,1,0.05},"PDF"]

Random[Real,{1,3}]

Table[Random[Real,{1,3}],{10}]

data=Table[Random[Real,{1,3}],{100000}];

Histogram[data,10,"PDF",PlotRange->{{0,5},All},AxesOrigin->{0,0}]

You will notice the property :

U[0, 1) a + b = U[b, b + a)

where equal “=” means equality in distribution. Once you can generate U[0, 1), you can generate random numbers that are uniformly distributed in arbitrary range.

Random numbers exponentially distributed (Mathematica)

Y = –(log U (0, 1])/g follows the exponential distribution with parameter g > 0. Let’s see this property heuristically.

data=Table[Random[],{100000}];

Take[data,10]

data2=-Log[data]/1.0;

Take[data2,10]

Min[data2]
Max[data2]

ghist=Histogram[data2,{0,13,0.1},"PDF"]

g=Plot[gamma Exp[- gamma x]/.gamma->1.0,{x,0,20},PlotStyle->RGBColor[1,0,0],PlotRange->All]

Show[ghist,g]

Random numbers with Gaussian (Normal) distribution (Mathematica)

In Mathematica, Random[NormalDistribution[0, 1]] generates a random number that follows N (0, 1).

data=Table[Random[NormalDistribution[0,1]],{10000}];

Min[data]
Max[data]

ghist=Histogram[data,50,"PDF"]

gauss=Exp[-(x-m)^2/2/sd^2]/Sqrt[2Pi]/sd/.{m->0, sd->1}
g=Plot[gauss,{x,-4,4},PlotStyle->RGBColor[1,0,0],PlotRange->All]

Show[ghist,g]

Random number of Normal distribution N (0, 1) can be generated by Box Muller method.

seq=Table[ Sqrt[-2Log[Random[]]]Cos[2Pi Random[]],10000]

Histogram[seq]

Poissoin process

Events that occur with the rate R (R > 0) occurs with time interval (waiting time) T that follows the exponential distribution with parameter R.

rateR=2

seq=Table[-Log[Random[]]/rateR,{10}]

The number of events that occurred within the unit time follows the Poisson distribution with parameter R.

The rate R corresponds to the average number of events that occurs within the unit time.