# Uniform Random Variables and Uniform Distribution

## Continuous Uniform Random Variables:

Continuous Uniform Random Variables are used to model a scenario where a continuous random variable can take values that are equally distributed (with equal probability) in an interval. Uniform distribution is the underlying distribution to model such random variables.

A continuous uniform distribution is denoted by

$latex X \sim \mathcal{U}(a,b) \text{, with a} \leq b &s=2$.

Here $latex a$ and $latex b$, are limits of the interval over which the distribution is effectively defined.

### Probability Density Function

The Probability Density Function (PDF) of the continuous uniform random distribution defined above is given by

This equation indicates that the distribution when used, generates values in the interval (a,b) with equal probability (with probability $latex \frac{1}{b-a}$)

### Continuous Density Function

The Continuous Density Function (CDF) can be derived from the above PDF using the relation

Therefore, the CDF of the continuous uniform distribution is given by,

Plotting the PDF and CDF,

## Discrete Uniform Random Variable:

Discrete Uniform Random Variables are used to model a scenario where a discrete random variable can take values that are equally distributed (with equal probability) in an interval. The underlying discrete uniform distribution is denoted by

$latex X \sim \mathcal{U}(\mathcal{S}) &s=2$

Where $latex \mathcal{S} $ is a finite set of discrete points on the real line. Meaning that each element in the finite set are equally likely.

### Example:

“Rolling a fair die” can be considered as an experiment to generate uniform integers in the range 1 to 6. Since rolling of a fair die give only one of the six possible integer values (1,2,3,4,5,6), it is an experiment that generates discrete values. Since all the values in a fair die are equally likely (with probability of getting each value =1/6) it can be modelled using a discrete uniform distribution.

For a die, the set $latex \mathcal{S} $ can take values {1,2,3,4,5,6}

### Probability Mass Function (PMF):

If , then the due to the definition of uniform distribution, each element in the set are equally probable. Therefore,

Then the PMF can be given by,

For rolling a fair die, the PMF simplifies as follows

The PMF and CDF for a discrete uniform random distribution is shown below

## Generating Uniform Random Numbers in Matlab:

### Generating Continuous Uniform Random Numbers:

In Matlab **“rand”** function is used to generate continous uniform random numbers in the range [0,1].

rand function is used to pick any number in the interval [0,1] in which all the numbers in the interval are equally likely.

**rand(n,m)** will generate a matrix of size nxm. A trial run at my system gave the following results

1 2 3 4 5 6 |
>>rand(3,4) >> ans = 0.2920 0.3900 0.4332 0.4159 0.0948 0.6802 0.8264 0.7536 0.6770 0.8449 0.6083 0.7715 |

To generate a random number in the interval [a,b] one can use the following code

** a + (b-a)*rand(n,m); %Here nxm is the size of the output matrix **

For example, to generate a matrix of size 3×4 using uniform distribution in the interval [2,10]

1 2 3 4 5 6 |
>> 2 + (10-2)*rand(3,4) >> ans= 5.4852 4.5016 9.6905 2.0585 2.7066 8.4537 9.8910 4.4816 3.1029 5.2562 6.9187 8.7710 |

### Testing Uniformity:

To test whether the numbers generated by the uniform distribution are uniform in the interval [a,b], one has to generate very large number of values using the rand function and then plot the histogram

Testing the uniformity of **rand** function in the interval [2,10]

1 2 |
>> x = 2+(10-2)*rand(1,100000); >> hist(x); |

The histogram plot given below shows that the numbers in the range [2,10] are generated with almost equal probability.

### Generating Discrete Uniform Random Numbers:

The same “rand” function can be used to generate discrete random numbers that follow discrete uniform distribution. The code to generate uniformly distributed discrete numbers from the set is given below.

1 |
r = ceil(n.*rand(1,100)); %This will generate 100 uniformly distributed discrete numbers on the set 1:n |

To model rolling of a fair die, the sample space . A roll of the die should generate a number from the sample space with equal probability. This single event can be modelled in matlab as

1 |
>> r = ceil(6.*rand(1,1)); |

(The uniformity test for discrete random numbers can be done using the same code given above in the previous section)

#### Note:There is one more function in Matlab called **“randn”**. This function is used to generate random numbers that are normally distributed.

## Reference:

[1] Matlab Documentation entry on generating Uniformly distributed pseudorandom numbers using rand function## See Also:

[1] Introduction to Random Variables, PDF and CDF[2] Chi-Squared distribution

[3] Derivation of Error Rate Performance of an optimum BPSK receiver in AWGN channel

[4] Eb/N0 Vs BER for BPSK over Rician Fading Channel

[5] BER Vs Eb/N0 for QPSK modulation over AWGN

[6] BER Vs Eb/N0 for 8-PSK modulation over AWGN

[7] Simulation of M-PSK modulation techniques in AWGN channel

[8] Performance comparison of Digital Modulation techniques

## Recommended Books

Pingback: Procedure for plotting any PDF in Matlab | GaussianWaves()

Pingback: Chi-Squared Distribution | GaussianWaves()

Pingback: Non-central Chi-squared Distribution | GaussianWaves()

Pingback: Simulation and Analysis of White Noise in Matlab | GaussianWaves()