Python's ability to generate random numbers is implemented in the random module. A pseudo-random number generator with various distributions is implemented
The module can generate 0 to 1 floating-point random numbers, and can be randomly selected in a sequence. The resulting random number can be evenly distributed. Gaussian distribution, logarithmic normal distribution. Negative exponential distribution and alpha. Beta distribution. However, these random numbers are not intended for use in encryption-based applications
You can also derive a subclass of the random class yourself to implement the random (), seed () in the subclass. GetState (), SetState () function, a new generator can provide a getrandbits () method. This agrees that Randrange () produces random numbers of arbitrary ranges.
Warning:
The random number in this module is a pseudo-random number and cannot be applied to secure encryption, assuming you need a true password security random number, you need to use Os.urandom () or the Systemrandom class in the random module to implement
Official documentation. The frequently used functions of the random module are as follows:
1, Random.random ()
Used to generate a random floating-point number from 0 to 1:0<= N < 1.0
2, Random.uniform (A, B)
Used to generate a random floating-point number within a specified range. One of the two parameters is the upper limit. One is the lower limit. Assuming a > B, the generated random number n:a <= n <= B. Suppose a <b, then B <= N <= A.
3, Random.randint (A, B)
Used to generate an integer within a specified range. The number of references a is the lower limit, the number of parameters is the upper limit, the number of random integers generated n:a <= n <= B
4, Random.choice (seq)
Gets a random element from the sequence. The parameter seqe represents an ordered type, which can be list,tuple,array. STR, etc.
5, Random.randrange ([Start,] stop[, step])
Gets a random number in the collection that increments by the specified cardinality, from within the specified range. such as: Random.randrange (10,100, 2), the result is equivalent from [10, 12, 14, 16, ... 96, 98] gets a random number in the sequence. Random.randrange (10,100, 2) is equivalent to Random.choice (range (10, 100, 2) on the result.
6, Random.shuffle (x[,random])
Used to disrupt elements in a list. can use this function to write a shuffle program
7, Random.sample (SEQ,K)
Gets a random fragment of the specified length from the specified sequence. The sample function does not change the original sequence, assuming that K is greater than the number of SEQ elements will be error.
>>> random.random () # random float x, 0.0 <= x < 1.00.37444887175646646>>> random.uniform (1, # Random float x, 1.0 <= x < 10.01.1800146073117523>>> random.randrange (Ten) # Integer from 0 to 97>>> random.randrange (0, 101, 2) # Even integer from 0 to 10026>>> random.choice (' Abcdefghij ')
# single random element ' C ' >>> items = [1, 2, 3, 4, 5, 6, 7]>>> random.shuffle (items) >>> items [7, 3, 2, 5, 6, 4, 1]>>> random.sample ([1, 2, 3, 4, 5], 3) # Three samples without replacement[4, 1, 5]
Python Random number