random random bag
>>> Import Random introducing random packet #
# take random integer >>> Random. The randint (1,10 ) . 4
# specified step length taken boast random integer >>> Random. Randrange (1, 10, ) randomly # between the odd-numbered 1-10 . 9 >>> random.randrange (0, 10 ) 2
# to random decimal between 0-1 >>> random. random () 0.25631948996842346 # take the specified interval random fractional >> > random. Uniform (1,10 ) 3.1196351125028334 >>> random.uniform (1,10 ) 5.857263369842762 >>> # a randomly chosen character >>> random.choice('abcdefg
' ) ' F ' # specified number of characters randomly >>> Random. Sample ( ' ABCDEFG ' ,. 3 ) [ ' F ' , ' D ' , ' E ' ]
# disrupted sort of random, only for a listing; original scrambled to: change the order of the original list, and does not return value returned is None >>> S = [1,2,3,4,5 ] >>> Random. shuffle (S) >>> S [ . 3 , 4, 1, 2, 5]
# ten randomly generated lowercase
>>> result =""
>>> for i in range(10):
... result +=chr(97+random.randint(1,25))
...
>>> print(result)
iiixvsfltw
# Generate a random number is not limited to the case of letters 10
>>> result=""
>>> lower_case_letter_num = random.randint(1,9)
>>> upper_case_letter_num = 10-lower_case_letter_num
>>> for i in range(lower_case_letter_num):
... result +=chr(ord("a")+random.randint(1,25))
...
>>> for i in range(upper_case_letter_num):
... result +=chr(ord("A")+random.randint(1,25))
...
>>> print(result)
ypskdkIDTN
string bag
>>> Import string # Import string package >>> the dir (string) # view the bag which method [ ' the Formatter ' , ' Template ' , ' _ChainMap ' , ' _TemplateMetaclass ' , ' __all__ is ' , ' __built ins__ ' , ' __cached__ ' , ' the __doc__ ' , ' __FILE__ ' , ' __loader__ ', '__name__', '__packag e__', '__spec__', '_re', '_string', 'ascii_letters', 'ascii_lowercase', 'ascii_u ppercase', 'capwords', 'digits', 'hexdigits', 'octdigits', 'printable', ' Punctua tion of ' , ' whitespace ' ] >>> string.ascii_letters # acquired all letters (uppercase and lowercase) ' ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz ' >>> string.ascii_lowercase # Get all lowercase letters ' ABCDEFGHIJKLMNOPQRSTUVWXYZ ' >>> string.ascii_uppercase # get all capital letters ' ABCDEFGHIJKLMNOPQRSTUVWXYZ ' >>> string.digits # get all digital ' 0123456789 '
Exercise:
# Generate the specified number of characters from random-zA-Z0-9 A: >>> string.ascii_letters ' ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz ' >>> string.digits ' 0123456789 ' >>> random.sample (string.ascii_letters + string.digits,. 8 ) [ ' n- ' , ' Q ' , ' F ' , ' . 7 ' , ' L ' , ' P ' , ' the I ' , 'and ' ] >>> ''.join(random.sample(string.ascii_letters+string.digits,8)) 'GgHXmqbj' >>> ''.join(random.sample(string.ascii_letters+string.digits,8)) 'XFCf2KvQ'