# Random use of various methods
Import Random
# random generation of floating-point numbers [0.1]
Print ("Random ():", Random.random ())
# randomly generates an integer between 1000-9999
Print ("Randint (9999):", Random.randint (1000, 9999))
# randomly generates an even number between 0-20
Print ("Randrange (0, 2):", Random.randrange (0, 21, 2))
# randomly generated floating-point numbers between 0-20
Print ("Uniform (0):", Random.uniform (0, 20))
# randomly select an element from the sequence
list_string = [' A ', ' B ', ' C ', ' d ', ' e ']
Print ("Choice (list):", Random.choice (list_string))
Print ("Choice (String):", Random.choice (' ABCD '))
# Random Ordering of list elements
List_number = [1, 2, 3, 4, 5]
Random.shuffle (List_number)
Print ("Shuffle (list):", List_number)
# randomly getting fragments of a specified length from a specified sequence
Print ("Sample (Sequence):", Random.sample (' ABCDEFG ', 2))
Operation Result:
Random (): 0.6708362810735843
Randint (1000, 9999): 5228
Randrange (0, 21, 2): 6
Uniform (0, 20): 12.767906137387294
Choice (list): a
Choice (string): D
Shuffle (list): [1, 3, 5, 2, 4]
Sample (Sequence): [' f ', ' G ']
Python's random function