?
This document uses PHP Chinese website manual Release
以下實例演示了如何生成一個隨機數(shù):
# -*- coding: UTF-8 -*- # Filename :test.py # author by : www.shouce.ren # 生成 0 ~ 9 之間的隨機數(shù) # 導入 random(隨機數(shù)) 模塊 import random print(random.randint(0,9))
以上代碼執(zhí)行輸出結果為:
4
以上實例我們使用了 random 模塊的 randint() 函數(shù)來生成隨機數(shù),你每次執(zhí)行后都返回不同的數(shù)字(0 到 9),該函數(shù)的語法為:
random.randint(a,b)
函數(shù)返回數(shù)字 N ,N 為 a 到 b 之間的數(shù)字(a