Python Random

The Python Random Library is used to introduce random numbers into your code. This tutorial explains how to use the Random library and it’s various functions.

Python Random Seed Function

Before we begin executing any random functions the random number generator must be initialized. By default, Python will use the system time to do so. However, we can also use the seed() function to customize the sequence.

The seed() function takes as input a number, and accordingly initializes the random number generator. (Using the same seed value will generate the same sequence.

import random

random.seed(5)
print(random.random())

random.seed(5)
print(random.random())
# OUTPUT
0.6229016948897019
0.6229016948897019

Python Random randint() Function

The randint() function returns a random integer from between the two given values.

import random

print(random.randint(1, 9))
print(random.randint(1, 9))
# OUTPUT
5
3

It’s first parameter is 1, the start point. The second parameter is 9, the end point.


Python Random randrange() Function

Almost identical to the randint() Function, with the addition of an extra optional parameter. This third parameter is the “increment” between random values produced. Default value is one.

print(random.randrange(3, 9,2))
print(random.randrange(3, 9,2))
print(random.randrange(3, 9,2))
print(random.randrange(3, 9,2))
print(random.randrange(3, 9,2))
print(random.randrange(3, 9,2))
5
3
7
5
3
5

As you can see, there is always a difference of 2 between all the values produced.


Python Random choice Function

The Choice() function picks a random value from a given sequence. Examples of such sequences are lists, strings, tuples, range or dictionaries.

dict1 = {1:"Apple", 2:"Cherry", 3:"Mango"}

print(random.choice(dict1))
list1 = ["Apple", "Grapes", "Bananas"]

print(random.choice(list1))
string = "PROGRAMMING"

print(random.choice(string))

Python Random Sample Function

The sample function takes from a sequence, a random sample of size “k”, where k is an integer. The returned value is in the form of a list.

mylist = ["Apples", "Bananas", "Cherry","Grapes","Mangos"]

print(random.sample(mylist, k=2))
print(random.sample(mylist, k=2))
['Cherry', 'Apples']
['Grapes', 'Bananas']

Python Random Shuffle Function

The shuffle() takes a sequence as it’s parameters, and shuffles the values in it randomly. Use with caution as it does not return a new list, instead altering the current one.

list1 = ["Apple", "Grapes", "Bananas","Grapes"]
random.shuffle(list1)
print(list1)
# OUTPUT
['Bananas', 'Apple', 'Grapes', 'Grapes']

Python Random random Function

The random function returns a random float value between 0.0 and 1.0.

print(random.random())
0.34632604177535975

Python Random Uniform Function

The uniform() function returns a random float value between two numbers. It takes these two numbers, the start and end point, as parameters.

print(random.uniform(1,100))
print(random.uniform(1,100))
# OUTPUT
67.71518613166376
19.740520613821747

Other Random Functions

The Python Random library has a lot of functions, however we’ve only covered the main ones in detail here. Some of the other lesser used ones are mentioned in the table below.

Note: Just because they are less used does not mean they are useless. The functions below are highly specialized and how niche uses. Only a small minority of people will actually ever need to use these.

FunctionDescription
getstate()Returns the current internal state of the random number generator
setstate()Restores the internal state of the random number generator
getrandbits()Returns a random number representing the bits given as input parameters.
choices()Returns a list with a random selection of elements from a sequence. Has the option for “weights” as well.
gauss()Returns a random float number between 0 and 1 based on the Gaussian distribution. Used often in machine learning and AI.
betavariate()Returns a random float number between 0 and 1 based on the Beta Distribution.
normalvariate() Returns a random float number between 0 and 1 based on the normal distribution. A concept also taught in statistics.
gammavariate() Returns a random float number between 0 and 1 based on the Gamma distribution.

Link to the Python Libraries Compilation: link
Link to the Random Library docs for more info: link

This marks the end of the Python Random Library Article. Suggestions and Contributions for CodersLegacy are more than welcome. Any relevant questions regarding this article can be asked in the comments section below.

Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments