## What is meant by random variables?

A random variable Z is a real valued function of the type Z: S → R on a sample space S. That means the function assigns a real number to every primitive event in the sample space. In other words, a random variable is a function mapping a sample space S to a set of real numbers R.

Consider a random variable Z having n different possible values. The sample space S = {z_{1}, z_{2}, …, z_{n}} will have probabilities P(Z = z_{i}) = p_{i}, where the probabilities must fulfil the following two conditions:

- $0\le {p}_{i}\le 1$ (The value of each probability should be less than 1.)
- ${p}_{1}+{p}_{2}+\dots +{p}_{n}=1$ (The sum of all the probabilities should be equal to 1.)

*Note: **A sample space is a collection of possible outcomes for a random event. It is usually represented with the symbol S, and the possible outcomes are stated within curly braces.*

## Example of a random variable

Consider an algorithm that computes the probability of obtaining the number of heads after tossing a coin.

Therefore, Z = {0,1,2}

Probability of 0 heads = $P\left(TT\right)=1/2*1/2=1/4$

Probability of 1 head = $P(HT|TH)=1/2*1/2+1/2*1/2=1/2$

Probability of 2 heads =$P\left(HH\right)=1/2*1/2=1/4$

Here,

- $0\le {p}_{1},{p}_{2},{p}_{3}\le 1$
- ${p}_{1}+{p}_{2}+{p}_{3}=\xbc+\xbd+\xbc=1$

Hence, both the conditions of random variables are fulfilled.

## Types of random variables

Random variables are generally categorized into the following two types:

- Discrete random variable
- Continuous random variable

### Discrete random variables

A random variable is known as discrete if it consists of a finite (countable) number of values. The probability distribution is stated as probability mass function (PMF) for such variables. Therefore,

P(z_{i}) = Probability of Z = z_{i} = PMF of Z = p_{i}

Further, the probabilities of the given random variable should satisfy the following rules:

- $0\le {p}_{i}\le 1$
- ${p}_{1}+{p}_{2}+\dots +{p}_{n}=1$

Here are a few examples of discrete random variables:

- The total number of trucks sold by a truck dealer in a specific month.
- The total number of people who have applied for a job in a company.
- The total number of students who attended college on a particular day.
- The number of eggs a hen laid on a particular day.
- The number of books sold by a book dealer in one month.
- The number of viewers who watched a badminton match on a particular date.

### Continuous random variables

Continuous random variables are variables that have an infinite (uncountable) number of values. The probability function for such variables is called a probability density function (PDF).

**Probability density function:**

If Z is a continuous random variable, where:

$P(zZz+dz)=f\left(z\right)*dz$

- $0\le f\left(z\right)\ge 1$, for every z
- $f\left(z\right)*dz=1$ over all values of z

Then, P(Z) will be the PDF of the distribution function.

A few examples of continuous random variables include:

- The total time that a car driver takes to reach San Franciso from Las Vegas.
- The deepness of drilling done to find crude oil.
- The weight of a vehicle at a vehicle-weighing station.
- The amount of rainfall in a village in a particular year.
- The average weight of a random group of 10 people.
- The height of the employees in a company.

## Formulas for random variables

The mean and variance of a random variable can be computed using the following formulas for a given data.

**Mean of a random variable**

If a random variable Z has P probabilities, the mean of the probability distribution can be measured using the following formula:

Mean $(\mu )=ZP$

Where random variable Z has all the possible values and P contains the respective probabilities.

**Variance of a random variable**

The variance of a random variable determines how much the variable has spread around the mean value. It is calculated using the following formula:

$Var\left(Z\right)={\sigma}^{2}=E\left({Z}^{2}\right)-{\left[E\right(Z\left)\right]}^{2}$,

Where $E\left({Z}^{2}\right)={Z}^{2}P$ and E(Z) = ZP

## Expected value of random variables

The expected value of a random variable Z is the sum of the product of the following two terms (for all the possible random events).

- Probability of that event
- Value of Z at that event.

Therefore, the expected value of random variable Z will be defined as:

$E\left(Z\right)={z}_{1}*{p}_{1}+{z}_{2}*{p}_{2}+\dots +{z}_{m}*{p}_{m}$

Z_{i} = Value of Z with probability p_{i}

For example:

When a six-faced dice is thrown the expected value will be as follows:

$=1*(\u2159)+2*(\u2159)+3*(\u2159)+4*(\u2159)+5*(\u2159)+6*(\u2159)\phantom{\rule{0ex}{0ex}}=3.5\phantom{\rule{0ex}{0ex}}$

## Use of random variables in computer science

The concepts of random variables are widely used in the machine learning field. For instance, neural network is used in machine learning to generate functions to determine the possible outcomes of an algorithm. Random variables are used to identify these outcomes.

## Randomized algorithm

Algorithms that utilize random variables while computing is called randomized algorithms. Such algorithms use random numbers to choose the next step in the logic. Due to the use of randomness, the space and time complexity of these algorithms is lesser than that of other algorithms. Examples of randomized algorithms include random quick sort and randomized binary search.

## Context and Applications

The random variables topic is included in data structure and algorithms as well as in probability in statistics. The concept is studied by students pursuing undergraduate and postgraduate courses like:

- Bachelor of Science in Computer Science
- Master of Science in Computer Science
- Bachelor of Science in computer engineering
- Bachelor of Science in statistics and data science

## Practice Problems

**Q1) **If E is the expectation, how will the variance of a random variable Z be denoted?

- $E\left({Z}^{2}\right)-{\left[E\right(Z\left)\right]}^{2}$
- $E\left(Z\right)-{\left[E\right(Z\left)\right]}^{2}$
- $E\left(Z\right)-\left[E\right(Z\left)\right]$
- $\sigma 2-E$

**Answer: **Option a

**Explanation:** According to the formula of variance of a random variable:

$\mathrm{Var}(Z)=E({Z}^{2})-{\left[E\right(Z)]}^{2}$

**Q2) **What is a random variable having an infinite number of values called?

- Long-run normal distribution interval
- Numerical density curve interval
- Discrete random interval variable
- Continuous random variable

**Answer: **Option d

**Explanation:** According to the definition, a continuous random variable is a random variable that has an infinite number of values.

**Q3) **What is a random variable having a countable number of values called?

- Bell-shaped random sample
- Discrete random variable
- Non-negative numerical variable
- Continuous random variable

**Answer: **Option b

**Explanation:** A random variable that has a finite (countable) number of values is referred to as a discrete random variable.

**Q4) **What is the probability distribution of a discrete random variable known as?

- Probability mass function
- Probability density function
- Uniform distribution probability
- All of the above

**Answer: **Option a

**Explanation: **The probability distribution function of discrete random variables is known as the probability mass function.

**Q5) **Which of the following conditions should be fulfilled by random variables?

- The sum of all probabilities should be equal to 1.
- The sum of all probabilities should be greater than 1.
- The sum of all probabilities should be lesser than 1.
- The sum of all probabilities can be any non-negative value.

**Answer: **Option a

**Explanation: **The probability distribution of random variables should fulfill two conditions: The sum of all probabilities should be equal to 1, and the value of each probability should not exceed 1.

## Common Mistakes

While solving questions related to random variables, students should make sure that the conditions for the probabilities are fulfilled.

## Related Concepts

- Noncentral chi-squared distribution
- Cumulative distribution function (CDF)
- Normal distribution
- Binomial Random Variable
- Bernoulli’s distribution

### Want more help with your computer science homework?

*Response times may vary by subject and question complexity. Median response time is 34 minutes for paid subscribers and may be longer for promotional offers.

### Search. Solve. Succeed!

Study smarter access to millions of step-by step textbook solutions, our Q&A library, and AI powered Math Solver. Plus, you get 30 questions to ask an expert each month.

# Random Variables Homework Questions from Fellow Students

Browse our recently answered Random Variables homework questions.

### Search. Solve. Succeed!

Study smarter access to millions of step-by step textbook solutions, our Q&A library, and AI powered Math Solver. Plus, you get 30 questions to ask an expert each month.