You should use these three methods from the random module

We all know python has a built-in module called random which can be used to generate random numbers and other random operations. We might have used some of the methods from the random module such as

  1. randint() – This method will take two numbers and return an integer value between those limits.
  2. choice() – This method will take an iterator and return an element from the iterator.
  3. random() – This method will generate a random float number.

This article explains about the following three underrated methods from the random module. They are

  1. shuffle()
  2. sample()
  3. SystemRandom()

Let us look at the above three methods one by one with an example for each.

shuffle()

The first method is a shuffle. The shuffle method basically takes an iterator and shuffle the items in the iterator. This method will not return anything. It alters or modifies the original iterator itself.

syntax

import random
random.shuffle(iterator)

Where it can be used?

The shuffle method can be used in the following scenarios.

  1. If you have a list, whose contents are to be shuffled.
  2. Getting random items from the list using a normal iteration.

Example

Let us look at this method with an example. I have a list of numbers and I want to shuffle the items in the list randomly.

shuffle method from the random module

The output of the above code is,

[4, 2, 5, 3, 1]

As you can see the contents of the list is shuffled randomly.

sample()

The next method is the sample() method. The sample() method can be used to return multiple values from an iterator. The sample() method will take two arguments.

  1. An iterator/range
  2. The number of values to be returned from the iterator.

syntax

import random
random.sample(iterator, integer)

Use cases

The sample() method has the following use cases.

  1. Multiple random values can be returned from an iterator.
  2. The returned values are random

Example

Let us look at the sample() method with an example for both iterator and range each.

sample method

The output of the above code is,

[1, 2, 5]

Since I have given the value 3 as the argument to the sample() method, three random elements are returned from the list.

We can also combine the sample() method with the range() method to get random numbers from a range. I will write a code to get 10 random numbers from the range 0 to 99.

sample and range method

The output of the above code is,

[39, 34, 86, 50, 66, 75, 97, 81, 59, 56]

10 random numbers are returned in the range 0-99.

SystemRandom

The last one is SystemRandom. This a class rather than a method. The main purpose of using this class is to generate truly random numbers. The randomness of this class is unpredictable and hence you must always choose to use this over the normal method. This is what the official documentation says about this class.

Class that uses the os.urandom() function for generating random numbers from sources provided by the operating system. Not available on all systems. Does not rely on software state, and sequences are not reproducible.

– python docs

All the random methods can be accessible with this class also.

Example

Let us look at the usage of this class.

SystemRandom() class

The output of the above code is,

64

Conclusion

Hope this article is helpful. For more pythonic ideas follow this blog regularly.

Happy coding!