## 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 = {z1, z2, …, zn} will have probabilities P(Z = zi) = pi, where the probabilities must fulfil the following two conditions:

•  (The value of each probability should be less than 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}

Here,

Hence, both the conditions of random variables are fulfilled.

## Types of random variables

Random variables are generally categorized into the following two types:

1. Discrete random variable
2. 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(zi) = Probability of Z = zi = PMF of Z = pi

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

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:

• , for every z
•  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

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:

,

Where 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:

Zi = Value of Z with probability pi

For example:

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

## 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?

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

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

1. Long-run normal distribution interval
2. Numerical density curve interval
3. Discrete random interval variable
4. Continuous random variable

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?

1. Bell-shaped random sample
2. Discrete random variable
3. Non-negative numerical variable
4. Continuous random variable

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?

1. Probability mass function
2. Probability density function
3. Uniform distribution probability
4. All of the above

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?

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

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.

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

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

We've got you covered with step-by-step solutions to millions of textbook problems, subject matter experts on standby 24/7 when you're stumped, and more.
Check out a sample computer science Q&A solution here!

*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.

Tagged in
EngineeringComputer Science