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 相关文章推荐
在python的类中动态添加属性与生成对象
Sep 17 Python
Python 3中print函数的使用方法总结
Aug 08 Python
Tensorflow的可视化工具Tensorboard的初步使用详解
Feb 11 Python
Python中反射和描述器总结
Sep 23 Python
python 常见字符串与函数的用法详解
Nov 23 Python
解决python3.5 正常安装 却不能直接使用Tkinter包的问题
Feb 22 Python
python3实现小球转动抽奖小游戏
Apr 15 Python
python绘制评估优化算法性能的测试函数
Jun 25 Python
Python+OpenCV+图片旋转并用原底色填充新四角的例子
Dec 12 Python
TensorFlow命名空间和TensorBoard图节点实例
Jan 23 Python
python实现扫雷游戏
Mar 03 Python
python-opencv 中值滤波{cv2.medianBlur(src, ksize)}的用法
Jun 05 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
PHP 和 HTML
2006/10/09 PHP
DISCUZ 分页代码
2007/01/02 PHP
解析PHP中数组元素升序、降序以及重新排序的函数
2013/06/20 PHP
php打开文件fopen函数的使用说明
2013/07/05 PHP
php实现微信公众平台账号自定义菜单类
2014/12/02 PHP
详解PHP中的状态模式编程
2015/08/11 PHP
教你识别简单的免查杀PHP后门
2015/09/13 PHP
js资料prototype 属性
2007/03/13 Javascript
各种效果的jquery ui(接口)介绍
2008/09/17 Javascript
使用javascript获取页面名称
2014/12/23 Javascript
总结javascript中的六种迭代器
2016/08/16 Javascript
JavaScript 继承详解(六)
2016/10/11 Javascript
2019 年编写现代 JavaScript 代码的5个小技巧(小结)
2019/01/15 Javascript
微信小程序实现滑动操作代码
2020/04/23 Javascript
Python实现按学生年龄排序的实际问题详解
2017/08/29 Python
Python向Excel中插入图片的简单实现方法
2018/04/24 Python
TensorFlow入门使用 tf.train.Saver()保存模型
2018/04/24 Python
Python实现曲线拟合操作示例【基于numpy,scipy,matplotlib库】
2018/07/12 Python
对pandas的层次索引与取值的新方法详解
2018/11/06 Python
Python的Tkinter点击按钮触发事件的例子
2019/07/19 Python
python元组和字典的内建函数实例详解
2019/10/22 Python
将数据集制作成VOC数据集格式的实例
2020/02/17 Python
利用 Python ElementTree 生成 xml的实例
2020/03/06 Python
Python实现迪杰斯特拉算法过程解析
2020/09/18 Python
Michael Kors加拿大官网:购买设计师手袋、手表、鞋子、服装等
2019/03/16 全球购物
英国森林假期:Forest Holidays
2021/01/01 全球购物
什么是封装
2013/03/26 面试题
优秀党员获奖感言
2014/02/18 职场文书
学习雷锋活动总结
2014/04/29 职场文书
立志成才演讲稿
2014/09/04 职场文书
期末考试复习计划
2015/01/19 职场文书
2015年前台个人工作总结
2015/04/03 职场文书
教师学习心得体会范文
2016/01/21 职场文书
2019入党申请书范文3篇
2019/08/21 职场文书
微信小程序结合ThinkPHP5授权登陆后获取手机号
2021/11/23 PHP
使用CSS实现黑白格背景效果
2022/06/01 HTML / CSS