# C++ normal_distribution高斯正态分布函数的用法示例

uniform_distribution 模板定义了可以产生随机浮点值的分布对象类型，默认是 double 类型。默认构造函数创建的是标准正态分布，因此期望是 0，方差是 1.0:

```std::normal_distribution<> dist; // mu: 0 sigma: 1
```

```double mu {50.0}, sigma {10.0};
std::normal_distribution<> norm {mu, sigma};
```

```std::random_device rd;
std::default_random_engine rng {rd()};
std::cout << "Normally distributed values: "<< norm (rng) << " " << norm (rng) << std::endl; // 39.6153 45.5608
```

```std::cout<<"mu: "<< norm.mean () << " sigma: " << norm.stddev ()<< std::endl; // mu: 50 sigma: 10
```

```using Params = std::normal_distribution<>::param_type; // Type alias for readability
double mu {50.0}, sigma {10.0};
std::normal_distribution<> norm {mu, sigma};// Create distribution
auto params = norm.param(); // Get mean and standard deviation
norm.param(Params {params.mean(),params.stddev() + 5.0}); // Modify params
std::cout << "mu: "<< norm.mean() << " sigma: " << norm.stddev ()<< std::endl; // mu: 50 sigma: 15
```

```using Params = std::normal_distribution<>::param_type; // Type alias for readability
std::random_device rd;
std::default_random_engine rng {rd()};
std::normal_distribution<> norm {50.0, 10.0}; // Create distribution
Params new_p {100.0, 30.0};// mu=100 sigma=30
std::cout << norm(rng, new_p) << std::endl; // Generate value with new_p: 100.925
std::cout << norm,mean() << " " << norm.stddev()<< std::endl;// 50 10
```

new_p 定义的期望和标准差只会被应用到它被作为第二个参数传入的 norm 的执行中。原始的期望和标准差会被应用到随后的没有第二个参数的 norm 调用中。

`std::cout << "min: " << norm.min () << " max: " << norm.max ()<< std::endl; // min:4.94066e-324 max: 1.7 9769e+308`