Home>Article>Backend Development> How to generate random numbers in python
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))].
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!