How to generate random numbers in python

王林
Release: 2022-01-12 15:30:02
Original
104471 people have browsed it

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))
Copy after login

Output results:

4
Copy after login

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)
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template