博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python 随机函数
阅读量:7115 次
发布时间:2019-06-28

本文共 815 字,大约阅读时间需要 2 分钟。

  hot3.png

随机整数:
>>> import random
>>> random.randint(0,99)
21
随机选取0到100间的偶数:
>>> import random
>>> random.randrange(0, 101, 2)
42
随机浮点数:
>>> import random
>>> random.random() 
0.85415370477785668
>>> random.uniform(1, 10)
5.4221167969800881
随机字符:
>>> import random
>>> random.choice('abcdefg&#%^*f')
'd'
多个字符中选取特定数量的字符:
>>> import random
random.sample('abcdefghij',3) 
['a', 'd', 'b']
多个字符中选取特定数量的字符组成新字符串:
>>> import random
>>> import string
>>> string.join(random.sample(['a','b','c','d','e','f','g','h','i','j'], 3)).r
eplace(" ","")
'fih'
随机选取字符串:
>>> import random
>>> random.choice ( ['apple', 'pear', 'peach', 'orange', 'lemon'] )
'lemon'
洗牌:
>>> import random
>>> items = [1, 2, 3, 4, 5, 6]
>>> random.shuffle(items)
>>> items
[3, 2, 5, 6, 4, 1]
random的函数还有很多,此处不一一列举。

转载于:https://my.oschina.net/renguijiayi/blog/272196

你可能感兴趣的文章
oracle数据库获取指定表的列的相关信息
查看>>
维克里拍卖 Vickrey auction
查看>>
Docker镜像的获取与删除
查看>>
Codeforces Round #370 (Div. 2) C. Memory and De-Evolution 水题
查看>>
别说无所谓
查看>>
Puppetmaster高可用和可扩展的方案设计
查看>>
[转载]ASP.NET伪静态页面的实现和伪静态在IIS7.0中的配置
查看>>
【SQL】SQL中笛卡尔积、内连接、外连接的数据演示
查看>>
HTTP解析
查看>>
MemCache超详细解读
查看>>
python numpy 教程
查看>>
手机web如何实现多平台分享
查看>>
策略模式和观察者模式
查看>>
详解CALayer 和 UIView的区别和联系
查看>>
eclipse中报错:java.lang.OutOfMemoryError: Java heap space
查看>>
Ubuntu 16.04 grub rescue 模式下修复 grub
查看>>
【Spring】24、<load-on-startup>0</load-on-startup>配置
查看>>
L0 Regularization
查看>>
使用JDBC向Kudu表插入中文数据乱码(转载)
查看>>
spf13-vim安装与使用
查看>>