This article to you to share is Python random number module of several common methods, very simple, small partners like the words, follow-up continue to further explore
">
The random module is a python-built module that has many features in addition to generating the simplest random numbers.
Random.random ()
Used to generate a random floating-point number between 0~1, range [0,10
>>> Import Random
>>> Random.random ()
0.5038461831828231
Random.uniform (A, B)
Returns a random floating-point number between a A and a, range [A, b] or [b], depending on rounding, 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 A and a, range [b], note: Incoming parameters must be integers, a must be smaller than B
>>> random.randint (50,100)
Si
>>> random.randint (100,50)
Traceback (most recent call last):
File "<pyshell#6>", line 1, in <module>
Random.randint (100
File "C:\Python27\lib\random.py", line 242, in Randint
return Self.randrange (A, b+1)
File "C:\Python27\l ib\random.py ", line 218, in Randrange
raise ValueError," empty range for Randrange () (%d,%d,%d) "% (Istart, Istop, WI DTH)
Valueerror:empty range for Randrange () (100,51, -49)
>>> random.randint (50.5,100.6)
Traceback (most recent):
File "<pyshell#7>", line 1, in <module>
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\r andom.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])
Returns an integer within a range that can be set to step. can only pass in integers, Random.randrange (10, 100, 2), resulting in the equivalent of [10, 12, 14, 16, ... 96, 98] gets a random number in the sequence.
>>> Random.randrange (100)
58
>>> Random.randrange (10,100,2)
54
Random.choice (Sequence)
Randomly get an element from a sequence, list, tuple, string all belong to sequence. Here the sequence need to be ordered type. Random.randrange (10,100,2) is equivalent to Random.choice (range (10,100,2) on 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 list of elements, commonly known as shuffle. The original sequence is modified.
>>> poker = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "Ten", "J", "Q", "K"]
>>> random.shuffle (poker)
>>> Poker
[' 4 ', ' Ten ', ' 8 ', ' 3 ', ' J ', ' 6 ', ' 2 ', ' 7 ', ' 9 ', ' Q ', ' 5 ', ' K ', ' A ']
Random.sample (Sequence,k)
A random fetch of k elements from the specified sequence is returned as a fragment, and the sample function does not modify the original sequence.
>>> poker = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "Ten", "J", "Q", "K"]
>>> Random.sample (poker,5)
[' 4 ', ' 3 ', ' Ten ', ' 2 ', ' Q ']
These are some of the ways Python is used, but there are a lot of stories about random numbers. Tell ~