This article will share with you several common methods of the Python random number random module, which are very simple, we will continue to explore in depth in the future. This article will share with you several common methods of the Python random number random module, which are very simple. if you like them, we will continue to explore them in depth in the future.
">
The random module is a Python module. in addition to generating the simplest random number, it also has many functions.
Random. random ()
Used to generate a 0 ~ Random floating point number between 1, in the range of [0, 10
>>> Import random
>>> Random. random ()
0.5038461831828231
Random. uniform (a, B)
Returns a random floating point number between a and B. The value range is [a, B] or [a, B). it is determined that a is not necessarily smaller than B.
>>> Random. uniform (50,100)
76.81733455677832
>>> Random. uniform (100,50)
52.98730193316595
Random. randint (a, B)
Returns an integer between a and B in the range of [a, B]. note: the input parameter must be an integer, and a must be smaller than B.
>>> Random. randint (50,100)
54
>>> Random. randint (100,50)
Traceback (most recent call last ):
File" ", Line 1, in
Random. randint (100,50)
File "C: \ Python27 \ lib \ random. py", line 242, in randint
Return self. randrange (a, B + 1)
File "C: \ Python27 \ lib \ random. py", line 218, in randrange
Raise ValueError, "empty range for randrange () (% d, % d, % d)" % (istart, istop, width)
ValueError: empty range for randrange () (, 51,-49)
>>> Random. randint (50.5, 100.6)
Traceback (most recent call last ):
File" ", Line 1, in
Random. randint (50.5, 100.6)
File "C: \ Python27 \ lib \ random. py", line 242, in randint
Return self. randrange (a, B + 1)
File "C: \ Python27 \ lib \ random. py", line 187, in randrange
Raise ValueError, "non-integer arg 1 for randrange ()"
ValueError: non-integer arg 1 for randrange ()
Random. randrang ([start], stop [, step])
Return an integer in the interval. you can set step. Only integers can be input. random. randrange (10,100, 2). The result is equivalent ,... 96, 98] obtain a random number from the sequence.
>>> Random. randrange (100)
58
>>> Random. randrange (10,100, 2)
54
Random. choice (sequence)
Obtain a random element from the sequence. the list, tuple, and string all belong to sequence. The sequence here must be of the ordered type. Random. randrange (10,100, 2) is equivalent to random. choice (range (10,100, 2) in the result.
>>> Random. choice ("stone", "scissors", "paper "))
'Stone'
>>> Random. choice (["stone", "scissors", "paper"])
'Scissors'
>>> Random. choice ("Random ")
'M'
Random. shuffle (x [, random])
Used to disrupt the elements in the list, commonly known as shuffling. The original sequence is modified.
>>> Poker = ["A", "2", "3", "4", "5", "6", "7", "8 ", "9", "10", "J", "Q", "K"]
>>> Random. shuffle (poker)
>>> Poker
['4', '10', '8', '3', 'J', '6', '2', '7', '9 ', 'q', '5', 'K', 'A']
Random. sample (sequence, k)
Returns k elements from a specified sequence as a segment. the sample function does not modify the original sequence.
>>> Poker = ["A", "2", "3", "4", "5", "6", "7", "8 ", "9", "10", "J", "Q", "K"]
>>> Random. sample (poker, 5)
['4', '3', '10', '2', 'Q']
These methods are commonly used in Python, but there are still many stories about random numbers. Next decomposition ~
The above is the details of the Python random number random module and the details of the instance. For more information, see other related articles in the first PHP community!