Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Special pages
Niidae Wiki
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Box–Muller transform
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Page information
Appearance
move to sidebar
hide
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Implementation== === C++ === The standard Box–Muller transform generates values from the standard normal distribution (''i.e.'' [[standard normal deviate]]s) with mean ''0'' and standard deviation ''1''. The implementation below in standard [[C++]] generates values from any normal distribution with mean <math>\mu</math> and variance <math>\sigma^2</math>. If <math>Z</math> is a standard normal deviate, then <math>X = Z\sigma + \mu</math> will have a normal distribution with mean <math>\mu</math> and standard deviation <math>\sigma</math>. The random number generator has been [[Random seed|seeded]] to ensure that new, pseudo-random values will be returned from sequential calls to the <code>generateGaussianNoise</code> function. <syntaxhighlight lang="cpp"> #include <cmath> #include <limits> #include <random> #include <utility> //"mu" is the mean of the distribution, and "sigma" is the standard deviation. std::pair<double, double> generateGaussianNoise(double mu, double sigma) { constexpr double two_pi = 2.0 * M_PI; //initialize the random uniform number generator (runif) in a range 0 to 1 static std::mt19937 rng(std::random_device{}()); // Standard mersenne_twister_engine seeded with rd() static std::uniform_real_distribution<> runif(0.0, 1.0); //create two random numbers, make sure u1 is greater than zero double u1, u2; do { u1 = runif(rng); } while (u1 == 0); u2 = runif(rng); //compute z0 and z1 auto mag = sigma * sqrt(-2.0 * log(u1)); auto z0 = mag * cos(two_pi * u2) + mu; auto z1 = mag * sin(two_pi * u2) + mu; return std::make_pair(z0, z1); } </syntaxhighlight> === JavaScript === <syntaxhighlight lang="javascript"> /* Syntax: * * [ x, y ] = rand_normal(); * x = rand_normal()[0]; * y = rand_normal()[1]; */ function rand_normal() { let theta = 2 * Math.PI * Math.random(); let R = Math.sqrt(-2 * Math.log(Math.random())); let x = R * Math.cos(theta); let y = R * Math.sin(theta); return [ x, y ]; } </syntaxhighlight> === Julia === <syntaxhighlight lang="julia"> """ boxmullersample(N) Generate `2N` samples from the standard normal distribution using the Box-Muller method. """ function boxmullersample(N) z = Array{Float64}(undef,N,2); for i in axes(z,1) z[i,:] .= sincospi(2 * rand()); z[i,:] .*= sqrt(-2 * log(rand())); end vec(z) end """ boxmullersample(n,μ,σ) Generate `n` samples from the normal distribution with mean `μ` and standard deviation `σ` using the Box-Muller method. """ function boxmullersample(n,μ,σ) μ .+ σ*boxmullersample(cld(n,2))[1:n]; end </syntaxhighlight>
Summary:
Please note that all contributions to Niidae Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Encyclopedia:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Search
Search
Editing
Box–Muller transform
(section)
Add topic