std:: generate_canonical
| Common mathematical functions | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Mathematical special functions (C++17) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Mathematical constants (C++20) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Basic linear algebra algorithms (C++26) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Data-parallel types (SIMD) (C++26) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Floating-point environment (C++11) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Complex numbers | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Numeric array (
valarray
)
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Pseudo-random number generation | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Bit manipulation (C++20) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Saturation arithmetic (C++26) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Factor operations | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Interpolations | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Generic numeric operations | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| C-style checked integer arithmetic | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
定义于头文件
<random>
|
||
|
template
<
class
RealType,
std::
size_t
Bits,
class
Generator
>
RealType generate_canonical ( Generator & g ) ; |
(C++11 起) | |
生成一个在范围
[
0
,
1
)
内的随机浮点数。
为生成足够的熵, generate_canonical ( ) 将精确调用 g ( ) \(\small k\) k 次,其中 \(\small k = \max(1, \lceil \frac{b}{\log_2 R} \rceil)\) k = max(1, ⌈ b / log 2 R ⌉) 且
- b = std:: min ( Bits, std:: size_t { std:: numeric_limits < RealType > :: digits } ) ,
- R = g. max ( ) - g. min ( ) + 1 。
目录 |
参数
| g | - | 用于获取熵值的生成器 |
返回值
浮点数值在范围
[
0
,
1
)
内。
异常
除 g 抛出的异常外,无其他异常。
注释
某些现有实现存在一个缺陷:当
RealType
为
float
时,可能偶尔会返回
1.0
GCC #63176
LLVM #18767
MSVC STL #1074
。该问题已被记录为
LWG issue 2524
。
示例
生成具有10位随机性的随机数:这可能会产生仅 k * R 个不同的值。
#include <iostream> #include <random> int main() { std::random_device rd; std::mt19937 gen(rd()); for (int n = 0; n < 10; ++n) std::cout << std::generate_canonical<double, 10>(gen) << ' '; std::cout << '\n'; }
可能的输出:
0.208143 0.824147 0.0278604 0.343183 0.0173263 0.864057 0.647037 0.539467 0.0583497 0.609219
参见
|
(C++11)
|
生成在范围内均匀分布的实数值
(类模板) |