Home  >  Article  >  Backend Development  >  Python随机生成数模块random使用实例

Python随机生成数模块random使用实例

WBOY
WBOYOriginal
2016-06-10 15:15:281464browse

代码

复制代码 代码如下:

#!/usr/bin/env python
#coding=utf-8
import random

#生成[0, 1)直接随机浮点数
print random.random()

#[x, y]中的随机整数
print random.randint(1, 100)

list = [1, 2, 3, 4, 5]
#随机选取
print random.choice(list)

#随机打乱
random.shuffle(list)
print list


输出
复制代码 代码如下:

0.787074152336
95
1
[4, 5, 2, 3, 1]
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