Home>Article>Backend Development> How to generate random numbers in python

How to generate random numbers in python

尚
Original
2019-07-20 15:42:09 32265browse

How to generate random numbers in python

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.

How to generate random numbers in python

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].

How to generate random numbers in python

If the random integer you want to generate does not contain b , that is, [a, b) , you can use the random.randrange() method.

How to generate random numbers in python

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.

How to generate random numbers in python

For more Python related technical articles, please visit thePython Tutorialcolumn 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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn