random module
random module
Random >>> Import # random decimal >>> random.random () # 0 is greater than 1 and less than a decimal between 0.7664338663654585 decimal >>> random.uniform (1,3) # 1 ≤ 3 1.6270147180533838
# Everwealth: fat red # random integer >>> random.randint (1,5) # 1 and less than or equal to an integer between. 5 >>> random.randrange (1, 10,) is greater than or equal to 1 and less than # 10 between the odd # randomly select a return >>> random.choice ([1, '23 ' , [4,5]]) # # 1 or 23 or a [4,5] # randomly selecting a plurality of return, the number returned is the second parameter of the function >>> random.sample ([1, '23 ' , [4,5]], 2) # # 2 list elements in any combination [[4, 5],' 23 '] # playing chaotic sequence listing >>> Item = [1,3,5,7,9] >>> random.shuffle (Item) # disarranged order >>> Item [. 5,. 1,. 3,. 7,. 9] >>> random.shuffle(item) >>> item [5, 9, 7, 1, 3]
Exercise: generating a random codes
import random def v_code(): code = '' for num=random.randint(0,9) alf add code="" return print
random module
Random >>> Import # random decimal >>> random.random () # 0 is greater than 1 and less than a decimal between 0.7664338663654585 decimal >>> random.uniform (1,3) # 1 ≤ 3 1.6270147180533838
# Everwealth: fat red # random integer >>> random.randint (1,5) # 1 and less than or equal to an integer between. 5 >>> random.randrange (1, 10,) is greater than or equal to 1 and less than # 10 between the odd # randomly select a return >>> random.choice ([1, '23 ' , [4,5]]) # # 1 or 23 or a [4,5] # randomly selecting a plurality of return, the number returned is the second parameter of the function >>> random.sample ([1, '23 ' , [4,5]], 2) # # 2 list elements in any combination [[4, 5],' 23 '] # playing chaotic sequence listing >>> Item = [1,3,5,7,9] >>> random.shuffle (Item) # disarranged order >>> Item [. 5,. 1,. 3,. 7,. 9] >>> random.shuffle(item) >>> item [5, 9, 7, 1, 3]
Exercise: generating a random codes
import random def v_code(): code = '' for i in range(5): num=random.randint(0,9) alf=chr(random.randint(65,90)) add=random.choice([num,alf]) code="".join([code,str(add)]) return code print(v_code())