Random Module In Python Programming Language

Random Module is a pre-installed library in the Python compiler. We don't need to install it in our System. In this post, we will discuss some functions of this random Module. Random modules generally use to make the Logic of games. In python, it is very easy To use the Random Module. let's see its functions.

random.randint()

random. randint() gives an integer between a number line. This function Requires 2 integer values. The first one is used as starting off the number line and the second one is used as the end of the Number line. 
Example : 
import random
rndm =  random.randint(12,20) //it gives the integer between 12 and 20
print(rndm)

random.randrange()

random. randrange() gives an integer but it does not require the starting point of a number line unlike random. randint() function if you don't want to give it a starting number point it assumes 0 as a starting point.
Example:
import random
rndm = random.randrange(19) //it gives the integer between 0 and 19
print(rndm)

random.choice()

random. choice() requires a list and gives the random element from this list. this function is used where you need to choose an option between different elements. 
Example : 
import random
list = ["stone", "paper","sissor]
rndm =  random.choice(list)
print(rndm)

random.shuffle()

random. shuffle() function requires a List and then this function set this list in random order. it is used to change the order in random order.
Example:
import random
list = ["stone","paper","sissor"]
rndm = random.shuffle(list) //set the list in different order
print(rndm)

random.random()

random.random() function requires nothing. But it gives us a random value between 0 and 1.
Example:
import random
rndm = random.random() //gives the value in between 0 AND 1 
print(rndm)

random.seed()

This function is used when we need a particular random value between 0 and1. this function requires an integer and gives us a pre-defined random value for this integer which we have passed in this random.seed() function. this function is applied only for random. random() function. You should apply this random.seed() function before the random.random() function.
Example: 
import random
random.seed(10)
rndm = random.random() // gives you only this value 0.5714025946899135
print(rndm)






 

Post a Comment

0 Comments