gpt4 book ai didi

c++ - uniform_real_distribution 所有可能的值生成

转载 作者:行者123 更新时间:2023-11-27 23:48:04 31 4
gpt4 key购买 nike

我目前正在研究重要性抽样,出于测试目的,我需要能够生成 uniform_real_distribution<float> 的所有可能值可能会在区间 [0,1] 内生成(是的,它也从右侧关闭)。我的想法是生成整数,然后我可以将其转换为 float 。从我所做的测试来看,[0,1] 中的统一单精度 float 和 [0,2^24] 中的整数之间似乎存在完美的双射(我对它不是 [0 ,2^24-1] 并且我仍在试图找出原因,我最好的猜测是 0 对 float 来说是特殊的,而 1 到 2^24 都会导致具有相同指数的 float )。我的问题是,以这种方式生成的 float 是否正是可以从 uniform_real_distribution<float> 生成的 float .您可以在下面找到我的整数 <-> float 测试:

void floatIntegerBitsBijectionTest()
{
uint32 two24 = 1 << 24;
bool bij24Bits = true;
float delta = float(1.0) / float(two24);
float prev = float(0) / float(two24);
for (uint32 i = 1; i <= two24; ++i)
{
float uintMap = float(i) / float(two24);
if (uintMap - prev != delta || uint32(uintMap*float(two24)) != i)
{
std::cout << "No bijection exists between uniform floats in [0,1] and integers in [0,2^24].\n";
bij24Bits = false;
break;
}
prev = uintMap;
}
if(bij24Bits) std::cout << "A bijection exists between uniform floats in [0,1] and integers in [0,2^24].\n";
std::cout << "\n";

uint32 two25 = 1 << 25;
bool bij25Bits = true;
delta = float(1.0) / float(two25);
prev = float(0) / float(two25);
for (uint32 i = 1; i <= two25; ++i)
{
float uintMap = float(i) / float(two25);
if (uintMap - prev != delta || uint32(uintMap*float(two25)) != i)
{
std::cout << "No bijection exists between uniform floats in [0,1] and integers in [0,2^25].\n";
if (i == ((1 << 24) + 1)) std::cout << "The first non-uniformly distributed float corresponds to the integer 2^24+1.\n";

bij25Bits = false;
break;
}
prev = uintMap;
}
if (bij25Bits) std::cout << "A bijection exists between uniform floats in [0,1] and integers in [0,2^25].\n";
std::cout << "\n";


bool bij25BitsS = true;
delta = 1.0f / float(two24);
prev = float(-two24) / float(two24);
for (int i = -two24+1; i <= two24; ++i)
{
float uintMap = float(i) / float(two24);
if (uintMap - prev != delta || int(uintMap*float(two24)) != i)
{
std::cout << i << " " << uintMap - prev << " " << delta << "\n";
std::cout << "No bijection exists between uniform floats in [-1,1] and integers in [-2^24,2^24].\n";
bij25BitsS = false;
break;
}
prev = uintMap;
}
if (bij25BitsS) std::cout << "A bijection exists between uniform floats in [-1,1] and integers in [-2^24,2^24].\n";
}

编辑:

有点相关:

https://crypto.stackexchange.com/questions/31657/uniformly-distributed-secure-floating-point-numbers-in-0-1

http://xoroshiro.di.unimi.it/random_real.c

https://www.reddit.com/r/programming/comments/29ducz/obtaining_uniform_random_floats_is_trickier_than/

https://lemire.me/blog/2017/02/28/how-many-floating-point-numbers-are-in-the-interval-01/

编辑 2:

我终于弄明白了 uniform_real_distribution<float>至少在与 mt19937 一起使用时engine 与它的默认模板参数一起使用时(我说的是 VS2017 附带的实现)。可悲的是,它只是在 [0,2^32-1] 中生成一个随机整数,将其转换为 float ,然后除以 2^32。不用说,这会产生非均匀分布的 float 。然而,我猜测这适用于大多数实际目的,除非人们正在接近生成数字之间增量的精度。

最佳答案

我假设 C++ 实现对 float 使用 IEEE-754 32 位基本二进制格式.在这种格式中,[1, 2] 中可表示的浮点值有规律地间隔,距离为 2−23

定义x与:

std::uniform_real_distribution<float> x(1, 2);

然后,假设 uniform_real_distribution实现得很好并且使用了合适的引擎,x(engine) - 1将为 [0, 223) 中的整数 n 生成等于 n/223 的值,具有统一分布。

注意事项

我对 uniform_real_distribution 的规范有疑虑在 C++ 中。它是根据实算术定义的。它返回具有恒定概率密度的值的要求需要一组连续的数字,而浮点格式不提供。此外,我不确定实现将如何处理端点。

由于分布被强制离散,所以不妨使用 uniform_int_distribution并将样本乘以 2−23(可作为 numeric_limits<float>::epsilon() 获得)。这样做的好处是可以根据需要阐明端点并轻松支持 [0, 1) 或 [0, 1] 的区间。

即使 C++ 标准不使用 IEEE-754,[1, 2] 中的可表示值也应该均匀分布,因为 C++ 标准中对浮点值的描述由 a 中的一些数字表示某个基数乘以基数的某个幂。对于零次幂,从 1 到 2 的值将根据格式中最低有效数字的值进行间隔。如上所述,该距离为 numeric_limits<float>::epsilon() .

脚注

1 C++ 标准使用旧术语“尾数”,但首选术语是“尾数”。

关于c++ - uniform_real_distribution<float> 所有可能的值生成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48968831/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com