A random variable is used in mathematics to study probability theory. It was developed to model the chance of events happening in all kinds of real-life scenarios.

Definition 
Take two measurable spaces, and name them  and . A measurable space is any pair of sets, called  and , that follow these rules:
  is not empty;
 The elements of  are subsets of ;
  and the empty set are both elements of ;
  is closed under complements and countable unions.
A random variable, named , is a measurable function from  to . This is written . A random variable is typically represented by capital roman letters such as , ,  and , and can be either discrete (taking on a countable set of values) or continuous (taking on an interval of values).

The set  is called the sample space. The set  is called the event space.

Use in Probability 
Take a probability measure  on . Take a set  in . Then  is defined to mean .

Examples with Dice 
When you roll a dice, 6 events can happen. These events are the different numbers that can show on the dice: 1, 2, 3, 4, 5, or 6.

Dicing Fruit 
Here is an example of a random variable. You roll a fair dice once. If the number on the dice is odd, you eat an apple, and if the number is even, you eat an orange. The random variable is the type of fruit that you will eat. Before you roll the dice, you don't know if you will eat an apple or an orange. We can also write this with mathematics:

Take . Take  to be the power set of . Take . Take  to be the power set of . Our random variable follows the even/odd rule from above:

Here  represents the number on the dice after you roll it. We said the dice is fair. In mathematics, this means  for  and .

The event that you eat an apple is a set in the event space. It is . The probability that you eat an apple is the probability measure of this set. It is .

Dicing Fruit Unfairly 
Here is another example. Sometimes a dice is unbalanced. This means that it rolls some numbers more often than others. We can do the same experiment as above, but with an unbalanced dice.

For example, let's say that we use a dice that has been changed in one way: it never rolls the number 5. Then you are less likely to eat an apple, because 5 is an odd number. The random variable is exactly the same as before:

This is because end possibilities have not changed. After the dice is rolled, you will eat an apple or an orange. You don't know which. What has changed is the probability that you eat an apple or an orange. In mathematics, this means

The event that you eat an apple is the same set as for the fair dice. It is . But this event is now less likely to happen, because the dice cannot roll a 5. In other words, the probability that you eat an apple is different than for the fair dice. It is .

Probability spaces 
The examples show that a random variable doesn't automatically give probabilities. If we choose  differently, one random variable can give different probabilities. This can be confusing. For this reason, mathematicians often define a random variable on a probability space. The mathematical notation for this is . This fixes the probability measure. Then there is no confusion about the probabilities of events.

Related pages 

 Expected value
 Probability density function

References

Further reading 
 Klenke, Achim, Probability Theory: A Comprehensive Course : Springer (2013) 

Mathematics