# random各种使用方法
import random # 随机生成[0.1)的浮点数
print("random():", random.random()) # 随机生成1000-9999之间的整数
print("randint(1000, 9999):", random.randint(1000, 9999)) # 随机生成0-20之间的偶数
print("randrange(0, 21, 2):", random.randrange(0, 21, 2)) # 随机生成0-20之间的浮点数
print("uniform(0, 20):", random.uniform(0, 20)) # 从序列中随机选择一个元素
list_string = ['a', 'b', 'c', 'd', 'e']
print("choice(list):", random.choice(list_string))
print("choice(string):", random.choice('abcd')) # 对列表元素随机排序
list_number = [1, 2, 3, 4, 5]
random.shuffle(list_number)
print("shuffle(list):", list_number) # 从指定序列中随机获取指定长度的片断
print("sample(sequence):", random.sample('abcdefg', 2))

运行结果:

random(): 0.6708362810735843
randint(1000, 9999): 5228
randrange(0, 21, 2): 6
uniform(0, 20): 12.767906137387294
choice(list): a
choice(string): d
shuffle(list): [1, 3, 5, 2, 4]
sample(sequence): ['f', 'g']

python之random函数的更多相关文章

  1. python的random()函数

    python 的random函数需要调用 #!/usr/bin/python # -*- coding: UTF-8 -*- import random print( random.randint(1 ...

  2. python的random函数

    Python中的random模块用于生成随机数.下面介绍一下random模块中最常用的几个函数. random.random random.random()用于生成一个0到1的随机符点数: 0 < ...

  3. Python的"random"函数的使用(一)

    random.randrange(1,10) 随机产生0~7之间的整数,不包含7. random.sample(range(100), 5) 随机从range(100)中产生5个数,放入一个list. ...

  4. python使用random函数生成随机数

    python使用random函数来生成随机数,常用的方法有: import random #生成0-1之间的数(不包括0和1) random.random() #生成1-5之间的随机整数(包括1和5) ...

  5. python的random模块生成随机数

    python的random函数 random.random() 生成0-1之间的随机数 random.uniform(a,b)生成a,b之间的浮点数 random.randint(a,b)生成a,b之 ...

  6. python标准库总的random函数用法

    Python标准库中的random函数,可以生成随机浮点数.整数.字符串,甚至帮助你随机选择列表序列中的一个元素,打乱一组数据等.random中的一些重要函数的用法:1 ).random() 返回0& ...

  7. python random函数

    .random模块方法说明 random.random()函数是这个模块中最常用的方法了,它会生成一个随机的浮点数,范围是在0.0~1.0之间. random.uniform()正好弥补了上面函数的不 ...

  8. Python random() 函数

    描述 random() 方法返回随机生成的一个实数,它在[0,1)范围内. 语法 以下是 random() 方法的语法: import random random.random() 注意:random ...

  9. python 内置函数!

    chr 数字转换字母 r = chr(65)print(r) ord字母转换数字n = ord("A")print(n) random 函数 import random li = ...

随机推荐

  1. csvwrite

    https://ww2.mathworks.cn/help/matlab/ref/csvwrite.html

  2. java 学习------helloword 程序测试

    1.新建一个helloword.java  文件 2.在新建的helloword.java 文件中添加以下代码 class helloWord { public static void main(St ...

  3. Python 爬取 11 万 Java 程序员信息竟有这些重大发现!

    一提到程序猿,我们的脑子里就会出现这样的画面: 或者这样的画面: 心头萦绕的字眼是:秃头.猝死.眼镜.黑白 T 恤.钢铁直男-- 而真实的程序猿们,是每天要和无数数据,以及数十种编程语言打交道.上能手 ...

  4. php的foreach中使用取地址符,注意释放

    先来举个例子: <?php $array = array(1, 2, 3); foreach ($array as &$value) {} // unset($value); forea ...

  5. Matplotlib 简单图例

    图例参考:http://matplotlib.org/gallery.html API参考:http://matplotlib.org/api/pyplot_summary.html # -*- co ...

  6. WebForms UnobtrusiveValidationMode requires a ScriptResourceMapping for 'jquery'. Please add a ScriptResourceMapping named jquery(case-sensitive).

    新开一个Web site.没有使用jQuery,当Insus.NET使用一些验证控件时,如RequiredfieldValidator,程序出现下面错误: WebForms UnobtrusiveVa ...

  7. RDLC报表显示图片

    有时设计RDLC报表时,我们会少不了在报表呈现图片. 今天花上些少时间来实现它们: 你可以在设计RDLC报表时,找到Report Data下的Image,按Mouse右键,出现Add Image... ...

  8. .Net Core 系列之一 hello world

    OS: win10 企业版 中文环境 .Net Core: 切记不要装64版本,否则可能会出现vs2017无法生成.net core 2.0的项目 dotnet-sdk-2.0.0-win-x86.e ...

  9. Luogu P1546 最短网络 Agri-Net

    其实这道题根本没必要写,但为了测试vector+堆优化的Prim试一发. 再次觉得Prim和Dijkstra很像,堆优化版本也差不多. 和Dijkstra一样,Prim也是在之前的dis点中选取一个最 ...

  10. [Spark][Python]sortByKey 例子

    [Spark][Python]sortByKey 例子: [training@localhost ~]$ hdfs dfs -cat test02.txt00002 sku01000001 sku93 ...