Random
Generate Random variables
Generates a random number of [0, 1) . Random
Import Randomrandom_number = Random.random () print (random_number) print (Random_number + 1)
Generates a [start, end] random number . Uniform
Import randomrandom_number = Random.uniform (0, ten) print (Random_number)
Generates a [start, end] interval random integer . Randint
Import Randomrandom_number = Random.randint (1, ten) print (Random_number)
Generate (start, end) interval random integer randrange
Import Randomrandom_number = Random.randrange (1, ten) print (Random_number)
Randomly take a value from a sequence choice
Import randomrandom_sequence = Random.choice (["1", "2", "3"]) print (random_sequence)
Sampling sample randomly from a sequence
Import randomtarget_str = "0123456789abcdefghijklmnopqrstuvwxyz" random_sequence = Random.sample (target_str, 6) print ( Random_sequence) # 6-digit random verification code print (". Join (Random_sequence))
Arithmetic Operation Verification Code implementation
Import randomnumber_str = "123456789" letter_str = ' abcdefghijklmnopqrstuvwxyz ' operator = "+-x" RANDOM_STR = Random.choice (NUMBER_STR) + random.choice (operator) + Random.choice (number_str) + random.choice (operator) + Random.choice (number_str) print (Random_str.replace (' x ', ' * ')) print (eval (random_str.replace (' x ', ' * ')))
python-Random Module-random