今天来点新花样,先复制一些英文如下:
Python Program to Generate a Random Number
In this example, you will learn to generate a random number in Python.
To understand this example, you should have the knowledge of the following Python programming topics:
Python Input, Output and Import
Python Random Module
To generate random number in Python, randint() function is used. This function is defined in random module.`# Program to generate a random number between 0 and 9
import random
print(random.randint(0,9))`
This returns a number N in the inclusive range [a,b], meaning a <= N <= b, where the endpoints are included in the range.
大凡有点基础的Python学习者,都使用过,或者至少知道这个又名而实用的模块,姑且叫它随机模块,我们用的最多的可能就是从一个正整数区间随机产生一个正整数数字,这个用于很多场合,比如下棋猜先,猜数字游戏,掷色子等都可以用这个模仿。
当然,还有随机选出列表中的一个元素,代码如下:
import random
list1 = [3,5,6,2,