f = uniform(.1, x)
while f > 1:
f = uniform(.1, x)
x应该是最小的值,以避免丢失精度和
减少呼叫uniform()的次数,例如:
import sys
# from itertools import count
# decimal.Decimal(1).next_plus() analog
# x = next(x for i in count(1) for x in [(2**BPF + i) / 2**BPF] if x > 1)
x = 1 + sys.float_info.epsilon
>>> for e in sorted(set(a)):
... print('{:.16e}: {}'.format(e,len(np.where(a==e)[0])))
...
1.0000000000000000e+00: 652
1.0000000000000002e+00: 1215
1.0000000000000004e+00: 1249
1.0000000000000007e+00: 1288
1.0000000000000009e+00: 1245
1.0000000000000011e+00: 1241
1.0000000000000013e+00: 1228
1.0000000000000016e+00: 1242
1.0000000000000018e+00: 640
然而,结合J.F.塞巴斯蒂安和马克·狄金森的评论,我认为这是可行的:
import numpy as np
import random
def rand_range(low=0,high=1,size=1):
a=np.nextafter(low,float('-inf'))
b=np.nextafter(high,float('inf'))
def r():
def rn():
return a+(b-a)*random.random()
_rtr=rn()
while _rtr > high:
_rtr=rn()
if _rtr<low:
_rtr=low
return _rtr
return [r() for i in range(size)]
如果以马克注释中的最小值分布运行,则离散浮点值非常少:
l,h=1,1+2**-48
s=10000
rands=rand_range(l,h,s)
se=sorted(set(rands))
if len(se)<25:
for i,e in enumerate(se,1):
c=rands.count(e)
note=''
if e==l: note='low value end point'
if e==h: note='high value end point'
print ('{:>2} {:.16e} {:,}, {:.4%} {}'.format(i, e, c, c/s,note))
你想要多精确的随机数?如果你喜欢10位十进制数字,你可以把
random.uniform(0.1, 1.0)
四舍五入到10位。这样您将同时包含0.1
和1.0
:准确地说,
0.1
和1.0
与介于两者之间的任何其他数字相比只有一半的概率,当然,您将丢失所有仅在10位之后不同的随机数。^{}
只是:
其中
self.random()
返回[0.0, 1.0)
范围内的随机数。Python(以及许多其他语言)使用floating point来表示real 数字。How ^{} is represented is described in detail in the
docs :
它允许使用
randint()
不丢失精度:randint(n, m)
返回[n, m]
(含)中的随机整数 因此,上述方法可能返回all浮点[0.1, 1]
中的数字。另一种选择是找到最小的
x
,这样x > 1
并使用:x
应该是最小的值,以避免丢失精度和 减少呼叫uniform()
的次数,例如:这两种解都保持了随机分布(no skew)的一致性。
你可以这样做:
或者,使用numpy's uniform:
nextafter将生成特定于平台的下一个可表示的朝某个方向的浮点数。使用numpy的random.uniform是有利的,因为它不包含上界是明确的。
编辑
马克·迪金森的评论似乎是正确的:Numpy's documentation关于random.uniform是否包含上界是不正确的。
Numpy文档声明
All values generated will be less than high.
这很容易被反驳:
在这个有限的范围内,结果也不一致:
然而,结合J.F.塞巴斯蒂安和马克·狄金森的评论,我认为这是可行的:
如果以马克注释中的最小值分布运行,则离散浮点值非常少:
它产生所需的均匀分布,包括端点:
在OP请求的值上,它还产生一个均匀分布:
输出
相关问题 更多 >
编程相关推荐