Data Science Certification Training with Pyth ...
- 86k Enrolled Learners
- Live Class
While creating software, our programs generally require to produce various items. This is most common in applications such as gaming, OTP generation, gambling, etc. Python makes the task of generating these values effortless with its built-in functions. This article on Random Number Generators in Python, you will be learning how to generate numbers using the various built-in functions.
Before moving on, let’s take a look at the topics discussed in this tutorial:
Generators are functions that produce items whenever they are called. Random Number Generator in Python are built-in functions that help you generate numbers as and when required. These functions are embedded within the random module of Python.
Take a look at the following table that consists of some important random number generator functions along with their description present in the random module:
|Values produced will be deterministic, meaning, when the seed number is the same, the same sequence of values will be generated|
|Can return random values between the specified limit and interval|
|Returns a random integer between the given limit|
|Returns a random number from a sequence|
|Shuffles a given sequence|
|Returns randomly selected items from a sequence|
|Returns floating-point values between the given range|
Now let us take a deeper look at each of these.
Random integers can be generated using functions such as randrange() and randint().
Let us first take a look at randint().
This function generates integers between a given limit. It takes two parameters where the first parameter specifies the lower limit and the second one specifies the upper limit. randint(a,b) starts generating values from a to b such that:
a <= x <= b (includes a and b)
import random random.randint(2,9)
The above code can generate numbers from 2 to 9 including the limits. In case you want to generate several values between this range, you can make use of the for loop as follows:
import random for x in range(2): print(random.randint(2,9))
In case you want to generate numbers in intervals, you can use the randrange() function.
The randrange() function, as mentioned earlier, allows the user to generate values by stepping over the interval count.
import random for x in range(5): print(random.randrange(2,60,2))
As you can see, all the numbers generated here are even numbers between 2 and 6.
You can also generate floating-point values using the built-in functions of the random module.
To generate floating-point numbers, you can make use of random() and uniform function.
This function produces floating-point values between 0.0 to 1.0 and hence, takes no parameters. Please note that the upper limit is excluded. So the maximum value will be 9.999.
import random for x in range(5): print(random.random())
Unlike random() function, this function takes two parameters that determine the lower and the upper limits respectively.
for x in range(5): print(random.uniform(6))
Python also allows you to generate random values from a given sequence as well.
This can be done using choice() and sample() functions.
This function basically takes a sequence as a parameter and returns random values from it.
for x in range(3): print(random.choice([1,2,3,4,5,6,7,8,9]))
As you can see, in the above output three values are returned using the for loop and all the values are taken randomly from the given list.
The sample() function picks up a random sequence from the given sequence and returns it as the output. It takes two parameters where the first parameter is a sequence and second is integer value specifying how many values need to be returned in the output.
OUTPUT: [1, 4, 5, 9]
As you can see, the output list produced in the above example consists of four randomly selected values from the given sequence.
The seed() function takes a number as a parameter called the seed and produces same random numbers each time you call this function with that number.
random.seed(2) print(random.random(),random.random(),random.random(),end='nn') random.seed(3) print(random.random(),random.random(),random.random(),end='nn') random.seed(2) print(random.random(),random.random(),random.random())
0.9560342718892494 0.9478274870593494 0.05655136772680869 0.23796462709189137 0.5442292252959519 0.36995516654807925 0.9560342718892494 0.9478274870593494 0.05655136772680869
In the above example, the output for seed(2) is the same each time it is called. This function is very useful in experiments where you need to pass the same random numbers to various test cases.
This function is used to shuffle a given sequence randomly.
mylist=[1,2,3,4,5,6,7,8,9] random.shuffle(mylist) print(mylist)
OUTPUT: [6, 8, 2, 4, 3, 7, 1, 5, 9]
This brings us to the end of this article on “Random number generator in Python”. I hope you have understood all the concepts.
Got a question for us? Please mention it in the comments section of this “Random Number Generator in Python” blog and we will get back to you as soon as possible.
To get in-depth knowledge on Python along with its various applications, you can enroll for live Python online training with 24/7 support and lifetime access.