C++:高精度随机双精度数
C++: High Precision Random Double Numbers
这是本页答案的后续问题:
如何在C++中高精度生成随机双精度数?
#include <iostream>
#include <random>
#include <iomanip>
int main()
{
std::random_device rd;
std::mt19937 e2(rd());
std::uniform_real_distribution<> dist(1, 10);
for( int i = 0 ; i < 10; ++i )
{
std::cout << std::fixed << std::setprecision(10) << dist(e2) << std::endl ;
}
return 0 ;
}
答案很好用,但我很难意识到如何将此代码的输出放在双精度变量中而不是将其打印到 stdout
中。谁能帮忙?
谢谢。
您在实际精度和显示精度之间感到困惑 - 试试这个:
#include <iostream>
#include <random>
#include <iomanip>
int main()
{
std::random_device rd;
std::mt19937 e2(rd());
std::uniform_real_distribution<> dist(1, 10);
double v = dist(e2); // get a random double at full precision
std::cout << std::fixed << std::setprecision(10) << v << std::endl;
// display random double with 10 digits of precision
return 0;
}
相关文章:
- 如何防止 c++ 在从浮点型转换为双精度型(不适用于 IO)时添加额外的小数?
- 正在将csv文件读取为双精度矢量
- 我可以信任表示整数的浮点或双精度来保持精度吗
- 如何在C++中的同一函数中使用字符串和双精度
- 特征::矩阵<双精度,1,3> 结构类型函数中的返回类型函数
- 检查是否以特定精度给出双精度
- 转换函数,将 std::数组的双精度作为参数或双精度作为参数单独转换
- C 字符串返回字符串的整数/双精度/长整型值
- 为什么将双精度转换为 int 似乎在第 16 位数字之后将其四舍五入?
- 如何使双精度值的 C++ 和 C# 中的结果相同
- 使用浮点数和双精度数的非常小数字的数学
- 使用 Xcode 将双精度存储在数组C++中
- 如何使用按位运算将随机uint64_t转换为范围 (0, 1) 的随机双精度
- 如何随机化双精度/浮点数据类型? C++
- 如何在 c++ 中生成介于 0 和变量之间的随机双精度
- C++:高精度随机双精度数
- 在不同范围内生成随机双精度
- 生成随机整型和双精度的c++模板类
- 在一系列值之间生成随机双精度
- 在 POSIX 上产生随机双精度的最佳方法是什么?