Python's random module and Pythonrandom Module
The random module in Python is used to generate random numbers. The following describes the most common functions in the random module.
Generate a 4-digit Verification Code randomly:
Import randomcheckcode = ''for I in range (4): # because it is a 4-digit verification code, it traverses four times. current = random. randrange () if current! = I: temp = chr (random. randint () # returns the ASCII characters corresponding to the integer I. Opposite to ord (), else: temp = random. randint () checkcode + = str (temp) # convert both numbers and ASCII characters into characters, that is, you can splice 'A' + '2' = 'a2 'print (checkcode)
Random. random
Random. random () is used to generate a random number of points 0 to 1: 0 <= n <1.0
Random. uniform
The prototype of the random. uniform function is random. uniform (a, B), which is used to generate a random number of characters in a specified range. One of the two parameters is the upper limit and the other is the lower limit. If a> B, the random number n: a <= n <= B. If a <B, B <= n <=.
- Printrandom. uniform (10, 20)
- Printrandom. uniform (20, 10)
- # ---- Results (results on different machines are different)
- #18.7356606526
- #12.5798298022
Print random. uniform (10, 20) print random. uniform (20, 10) # ---- result (different results on different machines) #18.7356606526 #12.5798298022
Random. randint
The prototype of the random. randint () function is random. randint (a, B), which is used to generate an integer in the specified range. Where parameter a is the lower limit, parameter B is the upper limit, and the generated random number n: a <= n <= B
- Printrandom. randint () # generated random number n: 12 <= n <= 20
- Printrandom. randint (20, 20) # The result is always 20
- # Printrandom. randint (20, 10) # This statement is incorrect. The lower limit must be less than the upper limit.
Print random. randint (12, 20) # generated random number n: 12 <= n <= 20 print random. randint (20, 20) # The result is always 20 # print random. randint (20, 10) # This statement is incorrect. The lower limit must be less than the upper limit.
Random. randrange
The prototype of the random. randrange function is random. randrange ([start], stop [, step]). a random number is obtained from the set that increments by the specified base number within the specified range. For example: random. randrange (10,100, 2), the result is equivalent to obtaining a random number from the [10, 12, 14, 16,... 96, 98] sequence. Random. randrange (10,100, 2) is equivalent to random. choice (range (10,100, 2) in the result.
Random. choice
Random. choice gets a random element from the sequence. The function prototype is random. choice (sequence ). The sequence parameter indicates an ordered type. Sequence is not a specific type in python, but a series of types. List, tuple, and string all belong to sequence. For sequence, see the python manual data model chapter. The following are examples of using choice:
- Printrandom. choice ("Learning Python ")
- Printrandom. choice (["JGood", "is", "a", "handsome", "boy"])
- Printrandom. choice ("Tuple", "List", "Dict "))
Print random. choice ("Learn Python") print random. choice (["JGood", "is", "a", "handsome", "boy"]) print random. choice ("Tuple", "List", "Dict "))
Random. shuffle
The original function of random. shuffle is random. shuffle (x [, random]), which is used to disrupt elements in a list. For example:
- P = ["Python", "is", "powerful", "simple", "andsoon..."]
- Random. shuffle (p)
- Printp
- # ---- Result (the results on different machines may be different .)
- # ['Powerful', 'simple', 'is', 'python', 'andsoon... ']
P = ["Python", "is", "powerful", "simple", "and so on... "] random. shuffle (p) print p # ---- result (the results on different machines may be different .) # ['Powerful', 'simple', 'is', 'python', 'and so on... ']
Random. sample
The prototype of the random. sample function is random. sample (sequence, k). Fragments of the specified length are randomly obtained from the specified sequence. The sample function does not modify the original sequence.
- List = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
- Slice = random. sample (list, 5) # randomly obtain 5 elements from the list and return them as a piece.
- Printslice
- Printlist # The original sequence has not changed.
Random INTEGER: >>> import random >>>> random. randint (100) 21 randomly selects an even number between 0 and: >>> import random >>> random. randrange (0,101, 2) 42 random floating point: >>> import random >>> random. random () 0.85415370477785668 >>> random. uniform (1, 10) 5.4221167969800881 random characters: >>> import random >>> random. choice ('abcdefg & # % ^ * F') 'd multiple characters to select a specific number of characters: >>> import randomrandom. sample ('abcdefghj', 3) ['A', 'D', 'B'] select a specific number of characters to form a new string: >>> import random >>>> import string >>> string. join (random. sample (['A', 'B', 'C', 'D', 'E', 'E', 'F', 'G', 'h', 'I ', 'J'], 3 )). replace ("", "") 'fih' randomly selects a string: >>> import random >>> random. choice (['apple', 'pear ', 'peach', 'Orange ', 'lemon']) 'limon' shuffling: >>> import random >>>> items = [1, 2, 3, 4, 5, 6] >>> random. shuffle (items) >>> items [3, 2, 5, 6, 4, 1]