How to use Random Module in Python

Random Module Function in Python

Random Module:-

Almost all module functions depend on the basic function random(), which generates a random float uniformly in the semi-open range [0.0, 1.0). Python uses the Mersenne Twister as the core generator. It produces 53-bit precision floats and has a period of 2**19937-1. The underlying implementation in C is both fast and thread safe. The Mersenne Twister is one of the most extensively tested random number generators in existence. However, being completely deterministic, it is not suitable for all purposes, and is completely unsuitable for cryptographic purposes.
The functions supplied by this module are actually bound methods of a hidden instance of the random.Random class. You can instantiate your own instances of Random to get generators that don’t share state.

So, You’ll learn About some Random Function with a example from the given below function:

• Random Floats
• Random Integers
• Random Number With Range
• Random Elements
• Random Shuffle Elements

So let’s start “How To Use Random Module Function in Python!

Random Floatsrandom.random() This method returns a random float between 0.0 to 1.0. The Function no need any Arguments.

Generate Random Floats Example:-

import random ## Always remember When You use Random Funtion always use import random
test = random.random() ## by default randomly floats show between 0.0 to 1.0
print(test)

Output:-

0.45376821697487113 ## 1st run randomly float output , now we run again
0.5567765979592608 ## 2nd Output , and you run again and again you show the always different output .

Random Integersrandom.randint() This Method use for random integers between the specific integers.

Generate Random Integers Example:-

import random
test = random.randint(1, 100) ## randint choose randomly integers from 1 to 100
print(test)

Outputs:

75 ## 1st integers Output
9 ## 2nd integers Output

Random Number With RangeThis Method use random.randrange()  returns a randomly selected element from the range created by the start, stop and step arguments. The value of start is 0 by default. Similarly, the value of step is 1 by default.

Random Number With Range Example:-

import random
test = random.randrange(1, 10) ## after run only show randomly output with between 1 to 10 but not including 10
print(test)

Output:-

1 ## 1st Output
9 ## 2nd Output

## we see more Example about range function .

import random
test = random.randrange(1, 10, 3) ## Now this is step argument and when run the program then every integers gap of 3 number
print(test)

Output:-

4 ## 1st Output
7 ## 2nd Output

Random Elements – random.choice(). This Method use for choose a randomly integers between given specific Integers and empty sequence raise indexerror.

Random Elements Example :-

import random
test = random.choice([90,10,20,50,80,40,70]) ## choose a randomly integers from the list
print(test)

Output:-

70 ## 1st Output
10 ## 2nd Output

## now we play with String for random.choice()

import random
test = random.choice(‘tiger’) ## choose a letter from the word and if you have 2 word and space between the word then also show blank space in output. (try yourself)
print(test)

e ## 1st Output
t ## 2nd Output

Random Shuffle Elements – random.shuffle() This method use for randomly reorder the all elements.

Random Shuffle Elements Example:-

import random
test = [3, 6, 9, 12, 15, 18, 21, 24, 27, 30] ## this is a list with order wise.
random.shuffle(test) ## now we use shuffle function use.
print(test)

Output:-

[27, 15, 24, 18, 30, 9, 21, 6, 12, 3] ## 1st Output reorder from the list
[24, 21, 18, 12, 9, 27, 3, 15, 6, 30] ## 2nd Output

Congratulations,today we learned “How To Use Random Module in Python!“.

Thank you ❤️