和java中的random()函数一样,在python中也有类似的模块random,即随机数

下面是我做的demo

运行效果:

==========================================

代码部分:

==========================================

 #python random

 import random

 def get_random():
'''get a random number.
return Random float x, 0.0 <= x < 1.0'''
return random.random() def get_uniform(a, b):
'''Return a random floating point number N such that
a <= N <= b for a <= b and b <= N <= a for b < a.
The end-point value b may or may not be included in the
range depending on floating-point rounding in the
equation a + (b-a) * random().
'''
return random.uniform(a, b) def get_randrange(n):
'''return an Integer from 0 to n
and the number n must be greater than 0
or n > 0
'''
return random.randrange(n) def get_randrange_ex(start, stop, step):
'''返回一个从start开始到stop结束,步长为step的随机数'''
return random.randrange(start, stop, step) def choice(s):
'''从一个字符串中随机获取一个字符串,传入的参数s是不能为空或者不能为None'''
if s != '' and s != None:
return random.choice(s)
else:
print('the param is empty or equals None!') def shuffle(items):
'''对一个序列进行洗牌的操作'''
random.shuffle(items)
return items def sample(items, n):
'''从一个序列中随机抽出n个数,当然,在这n个数中,可能出现有重复的数'''
return random.sample(items, n) def main():
r = get_random()
print('获取一个0.0-1.0之间的随机数:{}'.format(r))
r = get_uniform(2, 100)
print('获取一个2.0-100.0之间的随机数:{}'.format(r))
r = get_randrange(100)
print('获取一个0-100之间的随机数:{}'.format(r))
r = get_randrange_ex(3, 100, 25)
print('获取一个3-100之间的随机数:{}'.format(r))
tem_str = 'this is a test message!'
r = choice(tem_str)
print('从[{}]中随机取出一个字符:{}'.format(tem_str, r))
tem_items = [1, 2, 3, 4, 5, 6, 7]
tem_r = tem_items[:]
shuffle(tem_items)
print('对序列{}进行洗牌操作:{}'.format(tem_r, tem_items)) tem_list = sample(tem_r, 3)
print('从{}中随机抽出3个数:{}'.format(tem_r, tem_list)) if __name__ == '__main__':
main()

python开发_random的更多相关文章

  1. python开发环境搭建

    虽然网上有很多python开发环境搭建的文章,不过重复造轮子还是要的,记录一下过程,方便自己以后配置,也方便正在学习中的同事配置他们的环境. 1.准备好安装包 1)上python官网下载python运 ...

  2. 【Machine Learning】Python开发工具:Anaconda+Sublime

    Python开发工具:Anaconda+Sublime 作者:白宁超 2016年12月23日21:24:51 摘要:随着机器学习和深度学习的热潮,各种图书层出不穷.然而多数是基础理论知识介绍,缺乏实现 ...

  3. Python开发工具PyCharm个性化设置(图解)

    Python开发工具PyCharm个性化设置,包括设置默认PyCharm解析器.设置缩进符为制表符.设置IDE皮肤主题等,大家参考使用吧. JetBrains PyCharm Pro 4.5.3 中文 ...

  4. Python黑帽编程1.2 基于VS Code构建Python开发环境

    Python黑帽编程1.2  基于VS Code构建Python开发环境 0.1  本系列教程说明 本系列教程,采用的大纲母本为<Understanding Network Hacks Atta ...

  5. Eclipse中Python开发环境搭建

    Eclipse中Python开发环境搭建  目 录  1.背景介绍 2.Python安装 3.插件PyDev安装 4.测试Demo演示 一.背景介绍 Eclipse是一款基于Java的可扩展开发平台. ...

  6. Python开发:环境搭建(python3、PyCharm)

    Python开发:环境搭建(python3.PyCharm) python3版本安装 PyCharm使用(完全图解(最新经典))

  7. Python 开发轻量级爬虫08

    Python 开发轻量级爬虫 (imooc总结08--爬虫实例--分析目标) 怎么开发一个爬虫?开发一个爬虫包含哪些步骤呢? 1.确定要抓取得目标,即抓取哪些网站的哪些网页的哪部分数据. 本实例确定抓 ...

  8. Python 开发轻量级爬虫07

    Python 开发轻量级爬虫 (imooc总结07--网页解析器BeautifulSoup) BeautifulSoup下载和安装 使用pip install 安装:在命令行cmd之后输入,pip i ...

  9. Python 开发轻量级爬虫06

    Python 开发轻量级爬虫 (imooc总结06--网页解析器) 介绍网页解析器 将互联网的网页获取到本地以后,我们需要对它们进行解析才能够提取出我们需要的内容. 也就是说网页解析器是从网页中提取有 ...

随机推荐

  1. URAL题解一

    URAL题解一 URAL 1002 题目描述:一种记住手机号的方法就是将字母与数字对应,如图.这样就可以只记住一些单词,而不用记住数字.给出一个数字串和n个单词,用最少的单词数来代替数字串,输出对应的 ...

  2. 从LabVIEW到C++

    前言 最近一段时间一直没有更新,一方面是时间精力的问题(PS:懒癌犯了),另一方面是小黑大部分的时间都在学习C++相关知识,恶补了许多的知识(从大学C语言水平强制拔高了一段). 本文谈谈自己近期的一些 ...

  3. 7.Python3标准库--文件系统

    ''' Python的标准库中包含大量工具,可以处理文件系统中的文件,构造和解析文件名,还可以检查文件内容. 处理文件的第一步是要确定处理的文件的名字.Python将文件名表示为简单的字符串,另外还提 ...

  4. linux下查看资源使用情况

    //查看占用内存最多的前K的程序ps aux | sort -k4nr | head -K //查看占用CPU最多的前K的程序 ps aux | sort -k3nr | head -K

  5. cvc-complex-type.2.4.a: Invalid content was found starting with element ‘init-param’(转)

    在写xml的时候又一次总是报cvc-complex-type.2.4.a: Invalid content was found starting with element 错误,还出现小红叉,在网上找 ...

  6. centos7 mongodb3.4 安装

    上传tgz 安装包 [root@localhost install_pack]# ll total 274840 -rw-r--r--. 1 root root 9393241 Jun 2 14:36 ...

  7. SGU 209. Areas

    209. Areas time limit per test: 0.25 sec.memory limit per test: 65536 KB input: standardoutput: stan ...

  8. 【LOJ】 #2013. 「SCOI2016」幸运数字

    题解 最大异或和,明显是个线性基 然而还有那么多路径--那就树分治,反正点数看起来很少,就是为了让人乘上一个60的常数嘛 把一个树的点分树记录下来,然后看看询问的两个点彼此相同的最后一个父亲是谁,把这 ...

  9. 【LOJ】#2016. 「SCOI2016」美味

    题解 做了一下SCOI2015,于是决定搬运SCOI2016= v = 如果没有加法,我们可以向左向右节点查找 每个总权值是2^18 - 1,然后左右分,那么每次是一个完整的节点 如果有了加法,那么我 ...

  10. C++快速文件输入输出

    转载请注明: 仰望高端玩家的小清新 http://www.cnblogs.com/luruiyuan/ C语言可以获得接近汇编的性能,而输入输出常常是最为耗时的过程,因此可以使用 C 语言中的 fre ...