uniform_real_distribution<float> 所有可能的值生成
uniform_real_distribution<float> all possible values generation
我目前正在研究重要性采样,出于测试目的,我需要能够生成 uniform_real_distribution<float>
可能为区间 [0,1] 生成的所有可能值(是的,它从也对)。我的想法是生成整数,然后我可以将其转换为浮点数。从我所做的测试来看,[0,1] 中的统一单精度浮点数和 [0,2^24] 中的整数之间似乎存在完美的双射(我对它不是 [0 ,2^24-1] 并且我仍在试图找出原因,我最好的猜测是 0 对浮点数来说是特殊的,而 1 到 2^24 都会导致具有相同指数的浮点数)。我的问题是这样生成的浮点数是否正是可以从uniform_real_distribution<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";
}
编辑:
有点相关:
http://xoroshiro.di.unimi.it/random_real.c
https://lemire.me/blog/2017/02/28/how-many-floating-point-numbers-are-in-the-interval-01/
编辑 2:
我终于弄清楚了 uniform_real_distribution<float>
至少在与 mt19937
引擎一起使用时与它的默认模板参数一起使用时做了什么(我说的是 VS2017 附带的实现)。可悲的是,它只是在 [0,2^32-1] 中生成一个随机整数,将其转换为浮点数,然后除以 2^32。不用说,这会产生非均匀分布的浮点数。然而,我猜测这适用于大多数实际目的,除非一个人正在接近生成数字之间的增量精度。
你可以强制这个问题。滚动你自己的随机浮点生成器。
编辑:我刚刚发现 std::generate_canonical<float>()
,它做同样的事情,但不依赖于幻数 24。它从 std::numerical_limits<float>::digits
等推算出来...
#include <random>
static const unsigned long big = 1 << 24;
static std::default_random_engine re;
static std::uniform_int_distribution<unsigned long> uint(0, big - 1);
float rand_float() {
return uint(re) / static_cast<float>(big);
}
我假设 C++ 实现使用 float
的 IEEE-754 32 位基本二进制格式。在这种格式中,[1, 2] 中可表示的 floating-point 值规则间隔,距离为 2−23.
定义 x
为:
std::uniform_real_distribution<float> x(1, 2);
然后,假设 uniform_real_distribution
得到很好的实施并且使用了合适的引擎,x(engine) - 1
将生成等于 n / 2[=39= 的值]23 对于整数 n in [0, 223), 均匀分布.
备注
我对 C++ 中 uniform_real_distribution
的规范有疑虑。它是根据实算术定义的。它 return 具有恒定概率密度的值的要求需要一组连续的数字,而 floating-point 格式不提供。此外,我不确定实现将如何处理端点。
由于分布被迫离散,我们不妨使用 uniform_int_distribution
并将样本乘以 2−23(可用 numeric_limits<float>::epsilon()
).的好处是可以根据需要阐明端点并轻松支持 [0, 1) 或 [0, 1] 的区间。
即使 C++ 标准不使用 IEEE-754,[1, 2] 中的可表示值也应该均匀分布,因为 C++ 标准中描述了 floating-point 值由一些数字表示某个基数中的数字乘以基数的某次幂。对于零次幂,从 1 到 2 的值将根据格式中最低有效数字的值进行间隔。如上所述,该距离为 numeric_limits<float>::epsilon()
.
脚注
1 C++ 标准使用遗留术语“尾数”,但首选术语是“尾数”。
我目前正在研究重要性采样,出于测试目的,我需要能够生成 uniform_real_distribution<float>
可能为区间 [0,1] 生成的所有可能值(是的,它从也对)。我的想法是生成整数,然后我可以将其转换为浮点数。从我所做的测试来看,[0,1] 中的统一单精度浮点数和 [0,2^24] 中的整数之间似乎存在完美的双射(我对它不是 [0 ,2^24-1] 并且我仍在试图找出原因,我最好的猜测是 0 对浮点数来说是特殊的,而 1 到 2^24 都会导致具有相同指数的浮点数)。我的问题是这样生成的浮点数是否正是可以从uniform_real_distribution<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";
}
编辑:
有点相关:
http://xoroshiro.di.unimi.it/random_real.c
https://lemire.me/blog/2017/02/28/how-many-floating-point-numbers-are-in-the-interval-01/
编辑 2:
我终于弄清楚了 uniform_real_distribution<float>
至少在与 mt19937
引擎一起使用时与它的默认模板参数一起使用时做了什么(我说的是 VS2017 附带的实现)。可悲的是,它只是在 [0,2^32-1] 中生成一个随机整数,将其转换为浮点数,然后除以 2^32。不用说,这会产生非均匀分布的浮点数。然而,我猜测这适用于大多数实际目的,除非一个人正在接近生成数字之间的增量精度。
你可以强制这个问题。滚动你自己的随机浮点生成器。
编辑:我刚刚发现 std::generate_canonical<float>()
,它做同样的事情,但不依赖于幻数 24。它从 std::numerical_limits<float>::digits
等推算出来...
#include <random>
static const unsigned long big = 1 << 24;
static std::default_random_engine re;
static std::uniform_int_distribution<unsigned long> uint(0, big - 1);
float rand_float() {
return uint(re) / static_cast<float>(big);
}
我假设 C++ 实现使用 float
的 IEEE-754 32 位基本二进制格式。在这种格式中,[1, 2] 中可表示的 floating-point 值规则间隔,距离为 2−23.
定义 x
为:
std::uniform_real_distribution<float> x(1, 2);
然后,假设 uniform_real_distribution
得到很好的实施并且使用了合适的引擎,x(engine) - 1
将生成等于 n / 2[=39= 的值]23 对于整数 n in [0, 223), 均匀分布.
备注
我对 C++ 中 uniform_real_distribution
的规范有疑虑。它是根据实算术定义的。它 return 具有恒定概率密度的值的要求需要一组连续的数字,而 floating-point 格式不提供。此外,我不确定实现将如何处理端点。
由于分布被迫离散,我们不妨使用 uniform_int_distribution
并将样本乘以 2−23(可用 numeric_limits<float>::epsilon()
).的好处是可以根据需要阐明端点并轻松支持 [0, 1) 或 [0, 1] 的区间。
即使 C++ 标准不使用 IEEE-754,[1, 2] 中的可表示值也应该均匀分布,因为 C++ 标准中描述了 floating-point 值由一些数字表示某个基数中的数字乘以基数的某次幂。对于零次幂,从 1 到 2 的值将根据格式中最低有效数字的值进行间隔。如上所述,该距离为 numeric_limits<float>::epsilon()
.
脚注
1 C++ 标准使用遗留术语“尾数”,但首选术语是“尾数”。