Using Math and Random Modules

Practice Labs Module
Time
6 minutes
Difficulty
Intermediate

Welcome to the Using Math and Random Modules Practice Lab. In this module, you will be provided with the instructions and devices needed to develop your hands-on skills.

Join over 3 million cybersecurity professionals advancing their career
Sign up with
or

Already have an account? Sign In »

Overview

Introduction

Welcome to the Using Math and Random Modules Practice Lab. In this module, you will be provided with the instructions and devices needed to develop your hands-on skills.

Learning Outcomes

In this module, you will complete the following exercise:

  • Exercise 1 - Using the Math Module
  • Exercise 2 - Using the Random Module

After completing this lab, you will be able to:

  • Use built-in math functions in Python to perform various math operations such as addition, factorial, exponential, power, and square root
  • Generate random numbers using the random module
  • Demonstrate the use of 'seed' to produce pseudo-random numbers

Exam Objectives

The following exam objectives are covered in this lab:

  • 6.1 Perform basic operations using built-in modules
  • 6.2 Solve complex computing problems by using built-in modules

Lab Duration

It will take approximately 20 minutes to complete this lab.

Exercise 1 - Using the Math Module

Modules in Python refer to files containing Python statements and definitions that help solve a specific purpose in Python programs. Modules are a great way to manage large files and reuse code, thereby reducing programming effort and time.

Python has a lot of built-in modules. Math and random are two such built-in modules.

The math module has a lot of predefined functions to help perform common math operations such as addition, factorial, exponential, power, square root etc.

To use this module and its functions, all you need to do is import the module by using the** import** keyword as follows:

import math

Once that is done, you can access all functions defined in the module using the module name and the dot (.) operation. For example:

math.sqrt(4)

The above code will help you calculate the square root of 4 without writing a line of code.

The following table has a list of some of the functions and attributes defined in math module, which you will be practicing in this lab:

In this exercise, you will learn to write a program to perform simple mathematical operations using the built-in functions available in the math module.

Exercise 2 - Using the Random Module

Just like the math module, random is another module in Python. This module provides a fast pseudo-random number generator based on the Mersenne Twister algorithm, which is one of the most extensively tested random number generators in existence.

To use this module and its functions, first you need to import the module by using the import keyword as follows:

import random

Once that is done, you can access all functions defined in the module using the module name and the dot (.) operation. For example:

random.random()

The above code will generate a random floating-point number in the range [0.0, 1.0].

If, however, you want to generate random numbers within a specific range, you can use the random.uniform() function instead. For example:

random.uniform(200, 300)

The above code will generate random numbers between 200 and 300.

While printing the random numbers, you could also specify the format as follows:

print('%03.2f' % random.random())

print('%03.2f' % random.uniform(200, 300))

The format %03.2f specifies the following elements:

  • %f - This is the float value precision.
  • %.2f - This is the float value with two decimal precisions.
  • %03.2f - This is the float value with two decimal precisions and three digits to the left of decimal.

So, the random.random() function will generate numbers such as 0.80, 0.77 etc.

The output of the random.uniform() function will be something like 290.80, 218.57 etc.

Pseudo-random number generators perform some operations on a previous value. The previous value is mostly the previous number produced by the generator. However, when you use the generator for the first time, there is no previous value. In this case, you can provide the first value to the pseudo-random number generator. This is called seeding.

Each seed value corresponds to a sequence of random values generated for a given random number generator. As a result, if you provide the same seed value, you will get the same sequence of random numbers every time you run the code. The advantage of using a seed value is that you can use a specific seed to get a known sequence of numbers (therefore the name, pseudo-random).

By default, the seed value is the current time. If you use the default seed value, you will always get a different sequence of random numbers, which might get repeated after you run the code multiple times. However, if you want to get a known sequence of random numbers, you must pass a value, such as seed (10).

If you use the same seed value but different random number generators, such as random.random() or random.uniform(), you will get different sequences of random numbers for both these functions. This means that the sequence of random numbers generated by seed(10) with random,random() will produce one set of random numbers, while seed(10) with random.uniform() will produce a different set of random numbers. So, to get a known sequence of random numbers, you must use the same seed value with the same random number generator.

In this exercise, you will learn to:

  • Write a program to generate random numbers using the random module.
  • Write a program to demonstrate the use of 'seed' to produce pseudo-random numbers.

Learning Outcomes

After completing this exercise, you will be able to:

  • Generate random numbers using the random module
  • Demonstrate the use of 'seed' to produce pseudo-random numbers
Learning Partner
Comprehensive Learning

See the full benefits of our immersive learning experience with interactive courses and guided career paths.