Examples of common Python random numbers and random string methods, and python random numbers
Random INTEGER:
Copy codeThe Code is as follows:
>>> Import random
>>> Random. randint (0, 99)
21
Randomly select an even number between 0 and 100:
Copy codeThe Code is as follows:
>>> Import random
>>> Random. randrange (0,101, 2)
42
Random Floating Point Number:
Copy codeThe Code is as follows:
>>> Import random
>>> Random. random ()
0.85415370477785668
>>> Random. uniform (1, 10)
5.4221167969800881
Random characters:
Copy codeThe Code is as follows:
>>> Import random
>>> Random. choice ('abcdefg & # % ^ * F ')
'D'
Select a specific number of characters from multiple characters:
Copy codeThe Code is as follows:
>>> Import random
Random. sample ('abcdefghj', 3)
['A', 'D', 'B']
Select a specific number of characters to form a new string:
Copy codeThe Code is as follows:
>>> Import random
>>> Import string
>>> String. join (random. sample (['A', 'B', 'C', 'D', 'E', 'E', 'F', 'G', 'h', 'I ', 'J'], 3 )). r
Eplace ("","")
'Fih'
Randomly selected string:
Copy codeThe Code is as follows:
>>> Import random
>>> Random. choice (['apple', 'pear ', 'peach', 'Orange ', 'lemon'])
'Limon'
Shuffling:
Copy codeThe Code is as follows:
>>> Import random
>>> Items = [1, 2, 3, 4, 5, 6]
>>> Random. shuffle (items)
>>> Items
[3, 2, 5, 6, 4, 1]
There are still many random functions, which are not listed here,
References: http://docs.python.org/lib/module-random.html