問題描述
如何在 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++ 中生成服從正態分布的隨機數的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!