Precision to drand48
Precision to drand48
如何在C++中为drand48()添加精度?我在一个类似的功能中使用它
double x = drand48()%1000+1;
以生成低于1000的数字。但后来我得到了这个错误:
error: invalid operands of types ‘double’ and ‘int’ to binary ‘operator%’
当我使用时不会发生这种情况
double x = rand()%1000+1;
为什么rand()和drand48()有什么区别?
drand48
返回区间[0.0, 1.0)
的一个数字。你在找1到1000之间的数字吗?在这种情况下,您需要乘以999并加1。
事实上,你在期待什么?
drand48()
返回double
,而rand()
返回int
。
此外,drand48()
返回一个分布在[0.0, 1.0)
之间的值,因此您的公式需要更改:
double x = drand48() * 1000.0 + 1; // floating-point values from [1, 1001)
或
double x = (int)(drand48() * 1000.0) + 1; // integer values from [1, 1000]
您可以如上所述缩放drand48()
的结果,也可以将lrand48()
与现有公式一起使用。
drand48返回一个介于0.0到1.0之间的双精度。你想把它乘以你想要生成的范围。double x = drand48() * 1000.0
相关文章:
- "error: no matching function for call to"构造函数错误
- 调用专用模板时出错"no matching function for call to [...]"
- C++ Singleton - Prevent ::instance() to variable
- 我的项目不会像"undefined reference to `grpc::g_core_codegen_interface'"那样使用未定义的引用错误进行编译
- Visual Studio Code "undefined reference to `WinMain@16'"
- 使用 GCC 卸载的 OpenMP 卸载失败,并出现"Ptx assembly aborted due to errors"
- 如何解决"invalid conversion from 'char' to 'const char*'"
- 使用 MATLAB 编码器生成C++代码:编译错误"undefined reference to `rgb2gray_tbb_real64'"
- 尝试链接我的着色器时,我收到错误代码"error c5145 must write to gl_position"
- Python str to C++ to Python str
- 为什么创建友元类的实例会导致"undefined reference to"错误?
- System.InvalidCastException - SQL to C++ - safe_cast<float>
- 使用 cmake 的 Linux 终端上的"Conversion to non-scalar type is requested"错误
- "no matching function for call to 'Vector::Vector'"错误
- vector<vector<double>> to mxArray using memcpy
- Tensorflow c++ api undefined reference to 'tflite::D efaultErrorReporter()'
- 当覆盖存在时调用基本虚拟"binded to object"函数
- OpenGL VBO Indexing ( How to compute Index Array)
- 为什么我会" void value not ignored as it ought to be"?
- Precision to drand48