ImportRandomPrint(Random.random ())#randomly generates a decimal between 0-1Print(Random.randint (1,3))#randomly generates an integer between 1-3, including 1 and 3Print(Random.randrange (1,3))#randomly generates an integer greater than or equal to 1 and less than 3, excluding 3Print(Random.choice ([1,2,[3,5]))#randomly select a single or [3,5] from within the brackets .Print(Random.sample ([1,' at', [4,5]],3)]#any 3 combinations of list elementsPrint(Random.uniform (1,3))#randomly generates a decimal number greater than 1 and less than 3
Example: Generate a 4-bit verification code:
1 ImportRandom2Code_list = []3 forIinchRange (4):4NUM1 = Random.randint (0, 9)#randomly generate a 0-9 number.5str1 = Chr (Random.randint (65, 90))#randomly generate a number between 65-90 and then turn him into a letter6s =Random.choice ([num1,str1])7 Code_list.append (str (s))8Code ="'. Join (code_list)9 Print(code)
The random module of Python common modules