Table Of Contents:
Reference: https://docs.python.org/3/library/random.html
random
module implements pseudo-random generators for various distributions.
Some of the functions are as follows:
-
random.seed()
It initializes the random number generator.
-
random.randrange()
It accepts 3 parameters
start
,stop
, andstep
which is similar torange()
function. It returns the random from the eligible numbers from the range function. -
random.randint()
It is similar to
randrange()
method but thestop
number is inclusive -
random.choice()
It chooses a character from the string provided.
-
random.choices()
It chooses
k
number of characters as a list from the string provided.
import random
print(random.randint(1,6))
import random
for _ in range(10):
print(random.randint(1,6))
import random
options = "12345ABCDEabcde"
print(random.choice(options))
import random
options = "12345ABCDEabcde"
print(random.choices(options, k=5))
import random
options = "12345ABCDEabcde"
print(''.join(random.choices(options, k=8)))