TL;DR – The Python random module contains a set of functions for generating random numbers. The module allows you to control the types of random numbers that you create.
Why use the Python random module?
The random module can perform a few different tasks: you can use it to generate random numbers, randomize lists, or choose elements from a list at random.
This module is perfect for generating passwords or producing test datasets. It can also be integrated into Python
if loops to change the outcome of a function at random.
Generating Python random integers
The most basic and common use of the random module is to generate Python random integers. To accomplish this, you will need to import the random module and then use the
randint() Python function:
import random random.randint(0,10)
This will output a random number between
10, including end-points.
Alternatively, if you want a step size other than
1, you can use the
randrange() function. In this case, the syntax is:
random.randrange(start, stop[, step])
random.randrange() function uses a
step value of
1 by default. If you specify a
step, the range of potential outputs is determined using the Python
Generating random floating values
If you want to generate a random floating value rather than an integer, use the Python
import random random.random()
This will tell Python to generate a random number between
If you want a random float number between specific start and end values, use the
import numpy uniform = numpy.random.uniform(0, 100, size = (3, 5)) print(uniform)
This will tell Python to generate a random float number between
Random functions for lists and sequences
If you have a list of numbers, values, or other elements, you can use the Python random module to randomly select one or more elements. To choose a single element at random, use the
import random myList = ["bmw", "volvo", "toyota", "chrysler"] print(random.choice(myList))
If you want to pick more than one element from a list or sequence, use the
import random myList = ["bmw", "volvo", "toyota", "chrysler"] print(random.sample(myList, 3))
In the case that you have a list or sequence and you want Python to randomize the order of elements in the list for you, use the
import random myList = ["bmw", "toyota", "volvo", "chrysler"] random.shuffle(myList) print(myList)
Creating arrays of random numbers
If you need to create a test dataset, you can accomplish this using the
randn() Python function from the Numpy library.
randn() creates arrays filled with random numbers sampled from a normal (Gaussian) distribution between
The dimensions of the array created by the
randn() Python function depend on the number of inputs given. To create a 1-D array (that is, a list), enter only the length of the array desired. For example:
import numpy array = numpy.random.randn(3) print(array)
Similarly, for 2-D and 3-D arrays, enter the length of each dimension of the desired array:
import numpy array = numpy.random.randn(3, 5, 2) print(array)
It is possible to multiply the array generated by
randn() to get values outside of the default 0 to 1 range. Alternatively, you can use the
uniform() function to set upper and lower bounds on the random numbers generated:
import numpy array = numpy.random.uniform(0, 100, size = (3, 5)) print(array)
Python random: useful tips
- When using
random.random()to generate a random float number, you can multiply the result to generate a number outside the 0 to 1 range.
- If you want to be able to generate the same random number in the future, check the internal state of the random number generator using the
random.getstate(). You can reset the generator to the same state using the
Latest Coupon Found:
TOP-RATED CODING COURSES
Learn the skills of tomorrow
The best time to learn programming is now - follow this link to access 100+ coding courses and enjoy learning at a very low cost!