Python has many built-in modules or library which helps us with different functions or services. First when we say module or library, it is a collection of code library. We can relate it with a physical library, which holds different books which we served from it, python modules are also similar to that they help us with different functions.
So, one of those python modules is what we are going to see for now, which is the Random module, that we use to make random numbers or elements in different ways.
What Is The Python Random Module
This random module has several Methods and select an element(s) randomly from a non-empty sequence in different ways.
we are going to see some of them in the following format:
- Name of the method
- Their description
- Their function and implementation
- An example
In this article I will show a real example of random.choice() method that I use work to select names randomly from a list and assigns them with a randomly selected tasks.
First, let’s see pythons random randint() and randrange() methods to generate integer numbers from a given range randomly.
For example, let say you want to generate a random integer number between 10 to 20, you can use both randint() and randrange(), but we have to be careful which one to use, since they have a difference, we will see them one by one:
This randint() method requires both start and stop parameters. It returns then a random integer number given between start and stop and both are included.
random.randint(10, 20) return a random number between 10 & 20 (both 10 and 20 included)
You can have a good understanding from the following examples.
Again we don’t have to use numbers other than integers example, float: if used it will give you value error.
How To Use random.randint():
This random.randrange takes three parameters, in which, two of them are optional, these are the start and step are optional.
It generates a randomly selected number from the range, random.randrange(start, stop, step), in which stop is excluded.
Start parameter: is the starting number of the range. If not specified it will be 0 (zero) by default.
Stop parameter: is the last number in the range
Step parameter: is the different value between each number in elements or list that has to be returned. This parameter is optional. And if it is not specified as a parameter, the step value will be 1 by default.
Random.randrange(4, 18, 2), this will return:
Any random number between 4 and 18 in step of 2, like this; 4, 6, 8, 10……16, but 18 will not be returned in this case, because randrange() method doesn’t allow to return the last number in the range.
How To Use random.randrange():
From this above example, we see that the first one has only on argument, which means it is the last number, then the start number is zero by default, so any number including zero has equal probability to be selected except 15.
In the 2nd example, both start & stop numbers are given, so, any number between them has also equal probability to be returned, excluding the last number, in this case 20.
In the 3rd example, 3 arguments are given, in this case any number between 30 & 95 but in step of 5 has equal chance of selecting except 95.
Now, let’s see the random.choice(), radom.choices(), and random.sample() and their difference. After that we gonna see a real example with random.choice we used at work.
As we already saw in the above, random.randint(), and random.randrange() functions are used to return from a given range of integers, random.choice() function is used to select random element from a list, string or tuple which is from non-empty sequence.
- This random.choice() function returns a single element from the list
- Can’t choose from an empty list or string
- Passing an empty list to this function gives IndexError
Each name in the list has equal probability to be selected randomly, i.e.: 16.7%
Suppose we want to select multiple elements from a list, so, random.choice() is not an option, to do that we can use either random.choices(), or random.sample().
- Select multiple elements that we want
- Elements can be repeated if we use random.choices() , while
- Elements returned using random.sample() will not be repeated or no duplication.
Let’s see them one by one:
This function selects multiple elements depending on the sampling size you give.
Here, each element will have different probability of selection.
In this case, it is possible to specify “k” to select multiple elements from the list, other wise it will return only one element
Same as random.choices() function, random.sample() function also selects multiple elements from a given list, but without repeating any element.
Random.sample requires two arguments, as we see in the above syntax:
“k” is the sampling size, the number of elements we want to select. If you don’t specify “k” it will raise a type Error: missing 1 required positional argument.
To correct the error in the example above, you must include “k”.
Here is the correct way:
Randomly selecting employee names and assigning them a task:
At last, let me show you the real example I used to select names of employees randomly and assign a task for each one randomly without repeating anyone.
Importing the random module:
Employee names list:
Code + result of the code i.e. randomly selected names with a task assigned to them:
Therefore, this random module specially random.choice has an amazing function in the real working ground. We can even use it for example in football, champions league knockout round and such similar tasks.