
上QQ阅读APP看书,第一时间看更新
5.2.2 随机数函数
随机数可以用于数学、游戏、安全等领域中,还经常被嵌入算法中,用以提高算法效率,并提高程序的安全性。随机数函数的名称及功能说明如表5.2所示。
表5.2 随机数函数的名称及功能说明

随机数函数示例如下。
单击“开始”菜单,打开Python 3.6.5 Shell软件,然后在菜单栏中选择File→New File命令,创建一个Python文件,并命名为Python5-2.py,然后输入以下代码:

需要说明的是,首先要导入random模块,才可以使用该模块中的函数;否则程序会报错。
在菜单栏中选择Run→Run Module命令或按键盘上的F5键,就可以运行程序代码,结果如图5.2所示。

图5.2 随机数函数示例的运行结果