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

How to generate random numbers in python

王林
王林Original
2020-10-20 14:25:04104379browse

How to generate random numbers in Python: You can use the randint() function in the random module to generate random numbers, such as [import random print(random.randint(0,9))].

How to generate random numbers in python

Code implementation:

(Video tutorial recommendation: python video tutorial)

# -*- coding: UTF-8 -*-
 # Filename : test.py
 # 生成 0 ~ 9 之间的随机数
 # 导入 random(随机数) 模块
import random
print(random.randint(0,9))

Output results:

4

In the above example, we used the randint() function of the random module to generate random numbers. Each time you execute it, a different number (0 to 9) will be returned. This function The syntax is:

random.randint(a,b)

The function returns the number N, where N is a number between a and b (a <= N <= b), including a and b.

Related recommendations: python tutorial

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