Python生成随机数组的方法小结


Posted in Python onApril 15, 2017

本文实例讲述了Python生成随机数组的方法。分享给大家供大家参考,具体如下:

研究排序问题的时候常常需要生成随机数组来验证自己排序算法的正确性和性能,今天把Python生成随机数组的方法稍作总结,以备以后查看使用。

一、使用random模块生成随机数组

python的random模块中有一些生成随机数字的方法,例如random.randint, random.random, random.uniform, random.randrange,这些函数大同小异,均是在返回指定范围内的一个整数或浮点数,下边简单解释一下这几个函数。

1、random.randint(low, hight) -> 返回一个位于[low,hight]之间的整数

该函数接受两个参数,这两个参数必须是整数(或者小数位是0的浮点数),并且第一个参数必须不大于第二个参数

>>> import random
>>> random.randint(1,10)
5
>>> random.randint(1.0, 10.0)
5

2、random.random() -> 不接受参数,返回一个[0.0, 1.0)之间的浮点数

>>> random.random()
0.9983625479554628

3、random.uniform(val1, val2) -> 接受两个数字参数,返回两个数字区间的一个浮点数,不要求val1小于等于val2

>>> random.uniform(1,5.0)
2.917249424176132
>>> random.uniform(9.9, 2)
3.4288029275359024

*4、random.randrange(start, stop, step) -> 返回以start开始,stop结束,step为步长的列表中的随机整数,同样,三个参数均为整数(或者小数位为0),若start大于stop时 ,setp必须为负数.step不能是0.*

>>> random.randrange(1, 100, 2) #返回[1,100]之间的奇数
95
>>> random.randrange(100, 1, -2) #返回[100,1]之间的偶数
46

运行效果图如下:

Python生成随机数组的方法小结

5、生成随机数组

下边我们用random.randint来生成一个随机数组

import random
def random_int_list(start, stop, length):
  start, stop = (int(start), int(stop)) if start <= stop else (int(stop), int(start))
  length = int(abs(length)) if length else 0
  random_list = []
  for i in range(length):
    random_list.append(random.randint(start, stop))
  return random_list

接下来我们就可以用这个函数来生成一个随机的整数序列了

>>> random_int_list(1,100,10)
[54, 13, 6, 89, 87, 39, 60, 2, 63, 61]

二、使用numpy.random模块来生成随机数组

1、np.random.rand 用于生成[0.0, 1.0)之间的随机浮点数, 当没有参数时,返回一个随机浮点数,当有一个参数时,返回该参数长度大小的一维随机浮点数数组,参数建议是整数型,因为未来版本的numpy可能不支持非整形参数。

import numpy as np
>>> np.random.rand(10)
array([ 0.56911206, 0.99777291, 0.18943144, 0.19387287, 0.75090637,
    0.18692814, 0.69804514, 0.48808425, 0.79440667, 0.66959075])

当然该函数还可以用于生成多维数组,这里不做详述。

2、np.random.randn该函数返回一个样本,具有标准正态分布。

>>> np.random.randn(10)
array([-1.6765704 , 0.66361856, 0.04029481, 1.19965741, -0.57514593,
    -0.79603968, 1.52261545, -2.17401814, 0.86671727, -1.17945975])

3、np.random.randint(low[, high, size]) 返回随机的整数,位于半开区间 [low, high)。

>>> np.random.randint(10,size=10)
array([4, 1, 4, 3, 8, 2, 8, 5, 8, 9])

4、random_integers(low[, high, size]) 返回随机的整数,位于闭区间 [low, high]。

>>> np.random.random_integers(5)
4

5、np.random.shuffle(x) 类似洗牌,打乱顺序;np.random.permutation(x)返回一个随机排列

>>> arr = np.arange(10)
>>> np.random.shuffle(arr)
>>> arr
[1 7 5 2 9 4 3 6 0 8]
>>>> np.random.permutation(10)
array([1, 7, 4, 3, 0, 9, 2, 5, 8, 6])
Python 相关文章推荐
浅谈MySQL中的触发器
May 05 Python
python从入门到精通(DAY 2)
Dec 20 Python
python字符串连接方法分析
Apr 12 Python
Python爬取京东的商品分类与链接
Aug 26 Python
Python3中类、模块、错误与异常、文件的简易教程
Nov 20 Python
Python装饰器用法示例小结
Feb 11 Python
python3.6使用urllib完成下载的实例
Dec 19 Python
解决Python plt.savefig 保存图片时一片空白的问题
Jan 10 Python
Pytorch 抽取vgg各层并进行定制化处理的方法
Aug 20 Python
Pytorch 计算误判率,计算准确率,计算召回率的例子
Jan 18 Python
将 Ubuntu 16 和 18 上的 python 升级到最新 python3.8 的方法教程
Mar 11 Python
Python2.7:使用Pyhook模块监听鼠标键盘事件-获取坐标实例
Mar 14 Python
Python中文分词工具之结巴分词用法实例总结【经典案例】
Apr 15 #Python
Python结巴中文分词工具使用过程中遇到的问题及解决方法
Apr 15 #Python
Python编程实现生成特定范围内不重复多个随机数的2种方法
Apr 14 #Python
Python编程判断一个正整数是否为素数的方法
Apr 14 #Python
python编程实现归并排序
Apr 14 #Python
python实现折半查找和归并排序算法
Apr 14 #Python
Python+Wordpress制作小说站
Apr 14 #Python
You might like
FireFox浏览器使用Javascript上传大文件
2013/10/30 PHP
PHP获取youku视频真实flv文件地址的方法
2014/12/23 PHP
php 无限级分类 获取顶级分类ID
2016/03/13 PHP
关于laravel-admin ueditor 集成并解决刷新的问题
2019/10/21 PHP
浅谈laravel orm 中的一对多关系 hasMany
2019/10/21 PHP
JavaScript replace(rgExp,fn)正则替换的用法
2010/03/04 Javascript
JavaScript 异步方法队列链实现代码分析
2010/06/05 Javascript
javascript学习笔记(九)javascript中的原型(prototype)及原型链的继承方式
2011/04/12 Javascript
jquery动画1.加载指示器
2012/08/24 Javascript
页面装载js及性能分析方法介绍
2014/03/21 Javascript
使用AngularJS对路由进行安全性处理的方法
2015/06/18 Javascript
深入解析JavaScript编程中的this关键字使用
2015/11/09 Javascript
nodeJS删除文件方法示例
2016/12/25 NodeJs
Angular中的$watch、$watchGroup、$watchCollection
2017/06/25 Javascript
JavaScript鼠标拖拽事件详解
2020/04/03 Javascript
python编程开发之日期操作实例分析
2015/11/13 Python
浅析Python中的getattr(),setattr(),delattr(),hasattr()
2016/06/14 Python
Python入门_浅谈逻辑判断与运算符
2017/05/16 Python
Python中字典和集合学习小结
2017/07/07 Python
Python中一些不为人知的基础技巧总结
2018/05/19 Python
python 中文件输入输出及os模块对文件系统的操作方法
2018/08/27 Python
PySide和PyQt加载ui文件的两种方法
2019/02/27 Python
Python 串口读写的实现方法
2019/06/12 Python
python实现DEM数据的阴影生成的方法
2019/07/23 Python
django跳转页面传参的实现
2020/09/17 Python
解决pytorch 数据类型报错的问题
2021/03/03 Python
详解HTML5中rel属性的prefetch预加载功能使用
2016/05/06 HTML / CSS
英国灯具和灯泡网上商店:Lights.co.uk
2018/02/02 全球购物
GC是什么?为什么要有GC?
2013/12/08 面试题
医护人员英文求职信范文
2013/11/26 职场文书
干部选拔任用方案
2014/05/26 职场文书
我爱家乡演讲稿
2014/09/12 职场文书
教师党员个人整改措施材料
2014/09/16 职场文书
会计工作总结范文2014
2014/12/23 职场文书
2014年底个人工作总结
2015/03/10 职场文书
MySQL索引知识的一些小妙招总结
2021/05/10 MySQL