If you are in Python to generate random numbers and random modules in the most commonly used functions of the relationship and do not understand, the following article is the Python generation of random numbers and random modules most commonly used in the relationship between several functions, I hope you will be harvested, the following is the introduction of this article.
Random.random () is used to generate
Used to generate a number of random characters within a specified range, one of two parameters is the upper bound and one is the lower bound. If a > B, a random number is generated
N:a <= n <= B. If a <b, then B <= N <= A.
Print Random.uniform (
random.uniform)
#----
#18.7356606526
#12.5798298022
Used to generate an integer within a specified range. Where parameter A is the lower bound, parameter B is the upper bound, Python generates random numbers
#生成的随机数n of print Random.randint: <= n <=
print random.randint (#结果永远是20)
#print Random.ran Dint #该语句是错误的.
The lower bound must be less than the upper limit.
Random.randrange
From the specified range, in a collection that is incremented by the specified cardinality, this article is part of the application that Python generates random numbers.
Random integer:
>>> Import Random
>>> Random.randint (0,99)
21st
Randomly select an even number between 0 and 100:
>>> Import Random
>>> random.randrange (0, 101, 2)
42
Random floating-point numbers:
>>> Import Random
>>> Random.random ()
0.85415370477785668
>>> random.uniform (1, 10)
5.4221167969800881
Random characters:
>>> Import Random
>>> random.choice (' abcdefg&#%^*f ')
' d '
Select a specific number of characters in more than one character:
>>> Import Random
Random.sample (' Abcdefghij ', 3)
[' A ', ' d ', ' B ']
Select a specific number of characters to compose a new string in more than one character:
>>> Import Random
>>> Import String
>>> String.Join (Random.sample ([' A ', ' B ', ' C ', ' d ', ' e ', ' f ', ' g ', ' h ', ' I ', ' J '], 3)). R
Eplace ("", "")
' FIH '
Random Pick String:
>>> Import Random
>>> random.choice ([' Apple ', ' pear ', ' peach ', ' orange ', ' Lemon '])
' Lemon '
Shuffle:
>>> Import Random
>>> items = [1, 2, 3, 4, 5, 6]
>>> random.shuffle (items)
>>> Items
[3, 2, 5, 6, 4, 1]