Python implements the random element selection function, and python implements the element
To randomly select elements from the sequence, we can use the random. choice () method of the random module:
If you want to extract N elements, you can use the random. sample () method to further investigate the selected elements:
If we only want to disrupt the sequence (shuffling), we can use random. shuffle ():
To generate a random number, you can use the random. randint () method:
If you want to generate a floating point value evenly distributed between 0 and 1, you can use the random. random () method:
To obtain the integers represented by random bits of N, you can use the random. getrandbits () method:
Note:
The random module uses the Mersenne Twister (also called the Mason Rotation Algorithm) algorithm to calculate random numbers. This is a deterministic algorithm, but it can use more random. seed () function to modify the seed value
Random. seed () # based on the system time or OS. urandom () function random. seed (6666) # Based on the given integer random. seed (B 'bytes ') # Based on the given byte data
In addition to random numbers, the random module can also be used to calculate even distribution, Gaussian distribution, and other probability distributions.
Random. uniform # Calculate the uniform Distribution random. gauss () # Calculate the Gaussian distribution (normal distribution)
Note: Do not use the random module in encryption-related programs. If you have such application requirements, consider using functions in the ssl module for replacement. For example:
Ssl. RAND_bytes () # used to generate a random byte sequence for encryption security
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.