Home >Backend Development >Python Tutorial >python中随机函数random用法实例

python中随机函数random用法实例

WBOY
WBOYOriginal
2016-06-10 15:13:392036browse

本文实例讲述了python中随机函数random用法。分享给大家供大家参考。具体如下:

python中的random模块功能非常强大,可以生成各种随机值

#! python
# random
import random
print random.choice(['apple', 'pear', 'banana'])
#从数组中随机选择一个元素
print random.sample(xrange(100), 10)
# sampling without replacement
print random.random()
# random float
print random.randrange(6)
# random integer chosen from range(6)

希望本文所述对大家的Python程序设计有所帮助。

Statement:
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