美文网首页
1#2随机模块

1#2随机模块

作者: 淇漯草 | 来源:发表于2018-08-08 17:03 被阅读0次

python 产生随机数,随机字符串

import random


随机整数:

print random.randint(1,50)


随机选取0到100间的偶数:

print random.randrange(0, 101, 2)


随机浮点数:

print random.random()
print random.uniform(1, 10)


随机字符:

print random.choice('abcdefghijklmnopqrstuvwxyz!@#$%^&*()')


多个字符中选取特定数量的字符:

print random.sample('zyxwvutsrqponmlkjihgfedcba',5)


多个字符中选取特定数量的字符组成新字符串:

''.join(random.sample(['z','y','x','w','v','u','t','s','r','q','p','o','n','m','l','k','j','i','h','g','f','e','d','c','b','a'], 5))


随机选取字符串:

print random.choice(['AAAAA', 'BBBBB', 'CCCCC'])


打乱排序

items = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
print random.shuffle(items)

相关文章

  • 1#2随机模块

    python 产生随机数,随机字符串 import random 随机整数: print random.randi...

  • 2.系统模块上

    time时间模块 datetime模块 随机模块random demo: 取随机验证码 解决路径引用问题 os模...

  • Python模块学习之random模块

    random模块 产生随机数的模块 是Python的标准模块,直接导入即可 import random 1)随机取...

  • 深入理解random模块

    深入random模块 原创,转载需注明出处 python中random模块可以生成随机数或随机顺序或随机选择 我们...

  • python+AI第十一课

    随机数模块 随机数模块常用方法random.randint(a, b),返回a和b之间的随机整数 random.r...

  • 14.random模块

    random模块 1). random常用内置函数 引入random模块 返回随机整型,不包含末位值 随机返回In...

  • Python pygame之大球吃小球

    本次采用了pygame模块,random模块,math模块。random模块用于随机颜色math模块用了sqrt函...

  • python基础学习(三)

    常用模块 String模块 数学模块 随机模块 OS模块 os.path模块 re模块 常用函数及操作 列表操作 ...

  • 初级课程-乐高EV3教育版-行驶的小车系列011-随机模块

    本节课通过随机模块产生随机量作用于大型电机,通过不同的功率驱动小车前进。让同学们能够通过随机模块的使用掌握随机数在...

  • Python基础 random 模块

    random模块 python 中获取随机数的模块. 列出几个常用函数 random.random()随机一个 0...

网友评论

      本文标题:1#2随机模块

      本文链接:https://www.haomeiwen.com/subject/srknvftx.html