- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
random() * random()
和 random() ** 2
有区别吗? random()
从均匀分布中返回一个介于 0 和 1 之间的值。
在测试两个版本的随机平方数时,我注意到了一点不同。我创建了 100000 个随机平方数,并计算每个 0.01 区间(0.00 到 0.01,0.01 到 0.02,...)中有多少个数字。似乎这些版本的平方随机数生成是不同的。
对一个随机数求平方而不是将两个随机数相乘可以重复使用一个随机数,但我认为分布应该保持不变。真的有区别吗?如果不是,为什么我的测试显示出差异?
我为 random() * random()
生成两个随机分箱分布,为 random() ** 2
生成一个,如下所示:
from random import random
lst = [0 for i in range(100)]
lst2, lst3 = list(lst), list(lst)
#create two random distributions for random() * random()
for i in range(100000):
lst[int(100 * random() * random())] += 1
for i in range(100000):
lst2[int(100 * random() * random())] += 1
for i in range(100000):
lst3[int(100 * random() ** 2)] += 1
给出
>>> lst
[
5626, 4139, 3705, 3348, 3085, 2933, 2725, 2539, 2449, 2413,
2259, 2179, 2116, 2062, 1961, 1827, 1754, 1743, 1719, 1753,
1522, 1543, 1513, 1361, 1372, 1290, 1336, 1274, 1219, 1178,
1139, 1147, 1109, 1163, 1060, 1022, 1007, 952, 984, 957,
906, 900, 843, 883, 802, 801, 710, 752, 705, 729,
654, 668, 628, 633, 615, 600, 566, 551, 532, 541,
511, 493, 465, 503, 450, 394, 405, 405, 404, 332,
369, 369, 332, 316, 272, 284, 315, 257, 224, 230,
221, 175, 209, 188, 162, 156, 159, 114, 131, 124,
96, 94, 80, 73, 54, 45, 43, 23, 18, 3
]
>>> lst2
[
5548, 4218, 3604, 3237, 3082, 2921, 2872, 2570, 2479, 2392,
2296, 2205, 2113, 1990, 1901, 1814, 1801, 1714, 1660, 1591,
1631, 1523, 1491, 1505, 1385, 1329, 1275, 1308, 1324, 1207,
1209, 1208, 1117, 1136, 1015, 1080, 1001, 993, 958, 948,
903, 843, 843, 849, 801, 799, 748, 729, 705, 660,
701, 689, 676, 656, 632, 581, 564, 537, 517, 525,
483, 478, 473, 494, 457, 422, 412, 390, 384, 352,
350, 323, 322, 308, 304, 275, 272, 256, 246, 265,
227, 204, 171, 191, 191, 136, 145, 136, 108, 117,
93, 83, 74, 77, 55, 38, 32, 25, 21, 1
]
>>> lst3
[
10047, 4198, 3214, 2696, 2369, 2117, 2010, 1869, 1752, 1653,
1552, 1416, 1405, 1377, 1328, 1293, 1252, 1245, 1121, 1146,
1047, 1051, 1123, 1100, 951, 948, 967, 933, 939, 925,
940, 893, 929, 874, 824, 843, 868, 800, 844, 822,
746, 733, 808, 734, 740, 682, 713, 681, 675, 686,
689, 730, 707, 677, 645, 661, 645, 651, 649, 672,
679, 593, 585, 622, 611, 636, 543, 571, 594, 593,
629, 624, 593, 567, 584, 585, 610, 549, 553, 574,
547, 583, 582, 553, 536, 512, 498, 562, 536, 523,
553, 485, 503, 502, 518, 554, 485, 482, 470, 516
]
预期的随机误差是前两者之差:
[
78, 79, 101, 111, 3, 12, 147, 31, 30, 21,
37, 26, 3, 72, 60, 13, 47, 29, 59, 162,
109, 20, 22, 144, 13, 39, 61, 34, 105, 29,
70, 61, 8, 27, 45, 58, 6, 41, 26, 9,
3, 57, 0, 34, 1, 2, 38, 23, 0, 69,
47, 21, 48, 23, 17, 19, 2, 14, 15, 16,
28, 15, 8, 9, 7, 28, 7, 15, 20, 20,
19, 46, 10, 8, 32, 9, 43, 1, 22, 35,
6, 29, 38, 3, 29, 20, 14, 22, 23, 7,
3, 11, 6, 4, 1, 7, 11, 2, 3, 2
]
但第一个和第三个之间的差异要大得多,暗示分布不同:
[
4421, 59, 491, 652, 716, 816, 715, 670, 697, 760,
707, 763, 711, 685, 633, 534, 502, 498, 598, 607,
475, 492, 390, 261, 421, 342, 369, 341, 280, 253,
199, 254, 180, 289, 236, 179, 139, 152, 140, 135,
160, 167, 35, 149, 62, 119, 3, 71, 30, 43,
35, 62, 79, 44, 30, 61, 79, 100, 117, 131,
168, 100, 120, 119, 161, 242, 138, 166, 190, 261,
260, 255, 261, 251, 312, 301, 295, 292, 329, 344,
326, 408, 373, 365, 374, 356, 339, 448, 405, 399,
457, 391, 423, 429, 464, 509, 442, 459, 452, 513
]
最佳答案
下面是一些情节:
random() * random()
的所有可能性:
x轴是一个向右递增的随机变量,y轴是另一个向上递增的随机变量。
你可以看到,如果其中一个低,结果就会低,并且都必须高才能得到高结果。
当唯一的决策者是单个轴时,如 random() ** 2
案例,你得到
在这种情况下,它更有可能获得非常暗(大)的值,因为整个顶部都是暗的,而不仅仅是一个角。
当你用random() * random()
使两者都线性化时在顶部:
你看分布确实不一样。
代码:
import numpy
import matplotlib
from matplotlib import pyplot
import matplotlib.cm
def make_fig(name, data):
figure = matplotlib.pyplot.figure()
print(data.shape)
figure.set_size_inches(data.shape[1]//100, data.shape[0]//100)
axes = matplotlib.pyplot.Axes(figure, [0, 0, 1, 1])
axes.set_axis_off()
figure.add_axes(axes)
axes.imshow(data, origin="lower", cmap=matplotlib.cm.Greys, aspect="auto")
figure.savefig(name, dpi=200)
xs, ys = numpy.mgrid[:1000, :1000]
two_random = xs * ys
make_fig("two_random.png", two_random)
two_random_flat = two_random.flatten()
two_random_flat.sort()
two_random_flat = two_random_flat[::1000]
make_fig("two_random_1D.png", numpy.tile(two_random_flat, (100, 1)))
one_random = xs * xs
make_fig("one_random.png", one_random)
one_random_flat = one_random.flatten()
one_random_flat.sort()
one_random_flat = one_random_flat[::1000]
make_fig("one_random_1D.png", numpy.tile(one_random_flat, (100, 1)))
您也可以从数学上解决这个问题。获得小于 x
的值的概率, 与 0 ≤ x ≤ 1
是
random()²
:√x
作为随机值低于x
的概率是 random()² < x
的概率.
random() · random()
:给定第一个随机变量是r
第二个是R
,我们可以找到 Rr < x
的概率固定 R
:
P(Rr < x)
= P(r < x/R)
= 1 if x > R (and so x/R > 1)
or
= x/R otherwise
所以我们想要
∫ P(Rr < x) dR from R=0 to R=1
= ∫ 1 dR from R=0 to R=x
+ ∫ x/R dR from R=x to R=1
= x(1 - ln R)
正如我们所见,√x ≠ x(1 - ln R)
.
这些分布显示为:
y 轴给出直线(random()²
或 random() · random()
)小于 x 轴的概率。
我们看到 random() · random()
, 大数的概率明显较小。
我想最能说明问题的是区分(½x ^ -½
和 - ln x
)并绘制概率密度函数:
这显示了每个 x
的概率相对而言。所以 x
的概率很大 ( > 0.5
) 大约是 random()²
的两倍变体。
关于python - 为什么 random() * random() 与 random() ** 2 不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24479310/
在分析代码时,我偶然发现了以下代码段: msk = np.random.rand(len(df)) < 0.8 变量“msk”和“df”与我的问题无关。经过一些研究,我认为这种用法也与“随机”类有关。
出于几个合理的原因,我必须使用 BSD 的 random() 来生成非常大量的随机数,并且由于它的周期很短(~2^69,如果我没记错的话),这些数字的质量会降低对于我的用例来说很快。我可以使用我可以访
每种语言都有一个 random() 函数或类似的东西来生成伪随机数。我想知道下面会发生什么来生成这些数字?我没有编写任何需要这些知识的东西,只是想满足我自己的好奇心。 最佳答案 唐纳德·克努斯开创性的
我开发了一个简单的应用程序来生成测试数据系列,并且我使用随机种子将其构建为能够重复。我注意到以下情况并想知道为什么会这样: >>> random.seed(1) >>> [random.randint
random() * random() 和 random() ** 2 有区别吗? random() 从均匀分布中返回一个介于 0 和 1 之间的值。 在测试两个版本的随机平方数时,我注意到了一点不同
我发现 Python(及其生态系统)充满了奇怪的约定和不一致,这是另一个例子: np.random.rand Create an array of the given shape and popula
我想看看哪个随机数生成器包在我的神经网络中速度更快。 我目前正在从github上修改一段代码,其中numpy.random和random包都用于生成随机整数、随机选择、随机样本等。 我更改此代码的原因
我有一个 Python 大脚本。我在其他人的代码中启发了自己,所以我最终使用 numpy.random 模块来做一些事情(例如创建一个从二项分布中获取的随机数数组),在其他地方我使用模块 random
仅仅是因为“大型 API 综合症”还是生成在某些情况下更偏向的随机数?如果是……我认为控制偏见很重要。 最佳答案 他们是一样的,真的。只是一个方便的方法。检查 javadoc here .此外,您
我只是观察到,当使用 Python3 时,使用 random.shuffle 对列表进行洗牌需要大约一半的运行时间,而当为 显式提交函数 random.random >random 关键字参数。我检查
在python中随机模块,random.uniform()和random.random()有什么区别?它们都生成伪随机数,random.uniform() 生成均匀分布的数字,random.rando
是否可以在JMeter中生成“随机数”变量? 我已经记录了用户旅程 我已将旅程导入JMeter 我必须在用户旅程测试用例中输入一个唯一的4位数ID 在jmeter的当前默认值为2323 有没有一种方法
例如,如果我执行命令两次:ffmpeg -i input.mp4 -vf geq=r='random(1)*255':g='random(1)*255':b='random(1)*255' -stri
尽管随机生成器只创建一次,但输出始终是相同的随机结果(对于所有三个测试输出)。 来自稍大脚本的测试片段: let myRandGen = System.Random() let getRa
我正计划使用IntRange.random()(即(0..9999).random())在 Kotlin 中生成一个随机的5位代码。重要的是,恶意人员不能预测将要生成的数字的顺序。 IntRange.
您能否告诉我如何将 KDB 中的随机数生成器种子设置为或多或少的“随机”数? 我正在尝试执行以下操作: \S .z.i 但不知何故它不起作用。\S 似乎需要一个显式整数,而不是一个变量。 非常感谢!
我需要同时获得 /dev/random和 /dev/urandom在内核模块中。 get_random_bytes提供获取 /dev/urandom 的 API . 但是/dev/random 没有A
random.shuffle(lst_shuffle, random.random) 我知道后一部分是可选参数。但它究竟做了什么。我不明白这是什么意思。 这是来自文档。 random.random()
在树莓派 3 上: >>> import random >>> random.seed(0.9849899567458751) >>> random.random() 0.47871160253065
说我有一些python代码: import random r=random.random() r的值通常从哪里来? 如果我的操作系统没有随机数,那么它将在何处播种呢? 为什么不建议将其用于加密?有什么
我是一名优秀的程序员,十分优秀!