和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. docker安装总结 linux红帽系列

    由于Docker限制分为两个版本CE和EE,所以之前yum里面的docker是老版本,需要先进行卸载,现在的包名叫做docker-ce yum remove docker docker-common ...

  2. openjudge-NOI 2.6基本算法之动态规划 专题题解目录

    1.1759 最长上升子序列 2.1768 最大子矩阵 3.1775 采药 4.1808 公共子序列 5.1944 吃糖果 6.1996 登山 7.2000 最长公共子上升序列 8.2718 移动路线 ...

  3. 出现ERROR: While executing gem ... (Gem::FilePermissionError)这种错误的解决办法

    重新安装ruby即可解决 brew install ruby

  4. MySQL 操作总结

    1. 数据库级别操作 1.1 创建数据库 CREATE DATABASE db1 default charset utf8 collate utf8_general_ci; 1.2 删除数据库 DRO ...

  5. phpmailer发送邮件出现错误:stream_socket_enable_crypto(): SSL operation failed with code 1.

    如果开了调试,调试进去会看到错误提示: smtp_code:"stream_socket_enable_crypto(): SSL operation failed with code 1. ...

  6. windows下怎样使用md命令一次建立多级子目录

    在Windows系统中一次只能够创建一个子目录,在命令提示符窗口则可以一次性创建多个子目录,例如如果想在f盘创建多级子目录,则md 23\13\65\45,后面的数字随便都可以.如果想一次性删除多级目 ...

  7. 在C#中使用CURL

    private string args = "";          /// <summary>         /// 参数         /// </sum ...

  8. elementUI 学习入门之 radio 单选框

    Radio 单选框 基础用法 选项默认可见,选项不宜过多,选项过多建议使用 select 选择器 使用 Radio 组件,需要设置 v-model 绑定变量,选中意味着变量的值为相应 Radio  l ...

  9. 机器寻径引导算法C#(最短路径表)

    using System; using System.Collections; using System.Collections.Generic; using System.Linq; using S ...

  10. linux网络管理----Linux网络配置

    1.1 linux配置IP地址 1️⃣ ifconfig命令临时配置IP地址 能不能自动获取IP地址和DNS地址,要看有没有DHCP服务器 win10上,输入ipconfig /all 会看到是否有D ...