python:random.randint 和 numpy.random.uniform

1. random.randint(a,b): 随机生成一个a到b之间的一个整数(a<=n<=b)

用于生成一个指定范围内的整数。其中参数a是下限,参数b是上限,生成的随机数n:a<=n<=b

2. random.uniform(a,b):随机生成一个a到b之间的一个浮点数(a<=n<=b)

用于生成一个指定范围内的随机浮点数,两格参数中,其中一个是上限,一个是下限。如果a>b,则生成的随机数n,即b<=n<=a;如果a>b,则a<=n<=b。

3. numpy.random.uniform(low,high,size):输出size个low到high之间的数(low<=n<size)左闭右开

从一个均匀分布[low,high)中随机采样,注意定义域是左闭右开,即包含low,不包含high.

  • low: 采样下界,float类型,默认值为0;
  • high: 采样上界,float类型,默认值为1;
  • size: 输出样本数目,为int或元组(tuple)类型,例如,size=(m,n,k), 则输出m*n*k个样本,缺省时输出1个值。

4. 例:

import random
import numpy as np

a=0; b=7
index = random.randint(a, b)     #随机产生0到7之间的一个整数
print(index)

a=10; b=20
index = random.uniform(a,b)      #随机产生10到20之间的一个浮点数
print(index)

shape=17
cond = np.random.uniform(-0.1, 0.1, shape)    #随机产生-0.1到0.1之间的17个浮点数
print(cond)

#输出结果:
6
17.53995250541246
[ 0.08522486 -0.00211719  0.08933278 -0.00373567  0.06636708  0.0121164
  0.06903136  0.05635546 -0.07668082  0.0551379  -0.07817495  0.03939773
 -0.02098632 -0.07445089  0.0423184  -0.03277861  0.09023184]

#输出结果
4
15.57572931119358
[-0.07759161  0.00917166  0.02625699 -0.09831834  0.05971643 -0.09321461
 -0.0589061   0.06328565 -0.05303397 -0.07837874 -0.09519935  0.00481752
  0.09597165  0.08807344 -0.06250566 -0.07776539  0.00727126]

 

参考:

https://www.cnblogs.com/chamie/p/4917820.html    python--随机函数(random,uniform,randint,randrange,shuffle,sample)
https://blog.csdn.net/u013920434/article/details/52507173  numpy.random.uniform介绍

 
  • 6
    点赞
  • 56
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

weixin_39450145

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值