本文介绍了在 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++ 中生成服从正态分布的随机数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-21 12:54