This article mainly introduces the method of generating random numbers in Python.
If you don’t understand the relationship between generating random numbers in Python and the most commonly used functions in the random module, the following article is about generating random numbers in Python and the most commonly used functions in the random module. I hope you will gain something from the relationship between functions. The following is the introduction to this article.
Randomly generate a floating point number between 0 and 1
random.random() method will return a floating point number between [0.0, 1.0). Note that this It is an interval that is closed on the left and open on the right. The random number may be 0 but cannot be 1.
Randomly generate an integer between a and b
Using the random.randint(a, b) method, you can generate A random integer between a and b, that is, [a, b].
If the random integer you want to generate does not contain b , that is, [a, b) , you can use the random.randrange() method.
Generate a floating point number between a and b
If you need to generate a random floating point number between [a, b] points, then random.uniform(a, b) will suit your needs.
For more Python related technical articles, please visit the Python Tutorial column to learn!
The above is the detailed content of How to generate random numbers in python. For more information, please follow other related articles on the PHP Chinese website!