This article mainly introduces examples of common random numbers and random string methods in Python. This article describes common random methods, such as random integers, even numbers ranging from 0 to 100, random floating point numbers, and random strings, for more information, see
Random integer:
The code is as follows:
>>> Import random
>>> Random. randint (0, 99)
21
Randomly select an even number between 0 and 100:
The code is as follows:
>>> Import random
>>> Random. randrange (0,101, 2)
42
Random floating point number:
The code is as follows:
>>> Import random
>>> Random. random ()
0.85415370477785668
>>> Random. uniform (1, 10)
5.4221167969800881
Random characters:
The code is as follows:
>>> Import random
>>> Random. choice ('abcdefg & # % ^ * f ')
'D'
Select a specific number of characters from multiple characters:
The code is as follows:
>>> Import random
Random. sample ('abcdefghj', 3)
['A', 'D', 'B']
Select a specific number of characters to form a new string:
The 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:
The code is as follows:
>>> Import random
>>> Random. choice (['apple', 'pear ', 'peach', 'Orange ', 'lemon'])
'Limon'
Shuffling:
The 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