Generate random numbers following a normal distribution in C/C++(在 C/C++ 中生成服从正态分布的随机数)
问题描述
如何在 C 或 C++ 中轻松生成服从正态分布的随机数?
How can I easily generate random numbers following a normal distribution in C or C++?
我不想使用任何 Boost.
I don't want any use of Boost.
我知道 Knuth 详细谈到了这个问题,但我现在手头没有他的书.
I know that Knuth talks about this at length but I don't have his books at hand right now.
推荐答案
生成的方法有很多来自常规 RNG 的高斯分布数.
Box-Muller 变换 是常用的.它正确地产生具有正态分布的值.数学很容易.您生成两个(均匀)随机数,并通过对它们应用公式,您会得到两个正态分布的随机数.返回一个,并将另一个保存为下一个随机数请求.
The Box-Muller transform is commonly used. It correctly produces values with a normal distribution. The math is easy. You generate two (uniform) random numbers, and by applying an formula to them, you get two normally distributed random numbers. Return one, and save the other for the next request for a random number.
这篇关于在 C/C++ 中生成服从正态分布的随机数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 C/C++ 中生成服从正态分布的随机数


基础教程推荐
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 常量变量在标题中不起作用 2021-01-01
- 这个宏可以转换成函数吗? 2022-01-01
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 我有静态或动态 boost 库吗? 2021-01-01
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- C++结构和函数声明。为什么它不能编译? 2022-11-07