Random Module Function in Python
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 Floats – random.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
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 Integers – random.randint() This Method use for random integers between the specific integers.
Generate Random Integers Example:-
test = random.randint(1, 100) ## randint choose randomly integers from 1 to 100
75 ## 1st integers Output
9 ## 2nd integers Output
Random Number With Range –
This 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:-
test = random.randrange(1, 10) ## after run only show randomly output with between 1 to 10 but not including 10
1 ## 1st Output
9 ## 2nd Output
## we see more Example about range function .
test = random.randrange(1, 10, 3) ## Now this is step argument and when run the program then every integers gap of 3 number
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 :-
test = random.choice([90,10,20,50,80,40,70]) ## choose a randomly integers from the list
70 ## 1st Output
10 ## 2nd Output
## now we play with String for random.choice()
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)
e ## 1st Output
t ## 2nd Output
Random Shuffle Elements – random.shuffle() This method use for randomly reorder the all elements.
Random Shuffle Elements Example:-
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.
[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 ❤️