Python Random Module | List of functions in Random module

Here, we have discussed about the random module in python with list and how to create random integers and floats. Random module is an in-built...

Hey guys, welcome to the another most interesting article on Python programming language.

Python Random Module

Here, you will learn about the random module in python with list and how to create random integers and floats.

What is Random module?

Random module is an in-built module of python programming language which is used to generate random numbers. These numbers are pseudo-random numbers that means they are not truly random numbers. Random module is mainly used to generate random numbers, print random values, etc. 

Example ;

# import random

import random

# prints a random value from the list

Number = [1, 2, 3, 4, 5, 6]

print(random.choice(Number))

Output

5

List of functions in Random module

Here's the list of functions with brief description that comes under random module in python;

S.NoFunction Name Description
1. random() To return random floating numbers.
2. randint() To return a random number between a and b inclusive.
3. uniform() To return random floating numbers between given parameters.
4. seed()
Used to start the generator of random numbers.
5. choice() To return a random element from the given non-empty sequence.
6. choices() To return a multiple random elements with replacements.
7. randrange() To return a random number or integer from the range.
8. triangular() To return random floating number between two given numbers with a midpoint.
9. normalvariate() Used for Normal distribution.
10. weibullvariate() Used for Weibull Distribution.
11. sample() To return a list of sample from the chosen sequence.
12. lognormalvariate()
Used for Log Normal distribution.
13. setstate() Internal State of random number will be restored.
14. getstate() Used to return the current internal state with object of random number.
15. betavariate() Used for Beta distribution.
16. expovariate()
Used for Exponential distribution.
17. gammavariate() Used for Gamma distribution.
18. vonmisesvariate() Used for Vonmises distribution.
19. paretovariate() Used for Pareto distribution.
20. gauss() Used for Gaussian distribution.
21. getrandbits() Returns the python integer representing the random bits.
22. shuffle() To shuffle sequence or return the sequence in random order.

How to create Random Integers and Floats

With functions of random module we can create random integers and floats. In order to create random integers between a particular range, random.randint() function is used and to create random floats between 0.0 to 1, random.random() is used. 

Now let's illustrate Python integers and floats one by one with the help of following examples.

Python Random Integers Example;

# import random module

import random


# creating positive range

x = random.randint(7, 14)

print("The Random number between 7 and 14 is % s" % (x))


# creating negative range

y = random.randint(-15, -5)

print("The Random number between -15 and -5 is % d" % (y))

Output

The Random number between 7 and 14 is 9 
The Random number between -15 and -5 is -9

Python Random floats Example;

# import random

from random import random

# Prints random item

print(random())

Output

0.8215599925465855

Conclusion 

Above we have discussed about the random module in python with list and how to create random integers and floats. Random module is an in-built module of python programming language which is used to generate random numbers. With functions of random module we can create random integers and floats.