in Education by
How do I generate 3 Gaussian variables? I know that the Box-Muller algorithm can be used to convert two (U1,U2) uniform variables into two (X,Y) Gaussian variables but how do i generate the 3rd one? (Z). JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
A simple way: It is unlikely in this sort of game that you will need 3 Gaussian variates just once. You need some store variable that can contains either a triplet of Gaussian variates or nothing (Null, Nothing, Empty, whatever that is in your programming language, you didn't tell us which one). Initially, the store contains nothing (empty). When asked for a triplet: if the store contains a triplet, just return that triplet. And mark the store as empty. if the store is empty, run Box-Muller 3 times. That gives you 2 triplets. Put the second triplet in the store. Return the first triplet. An alternative way for the mathematically inclined programmer: If one just tries to adapt Box-Muller to 3 dimensions, the sole tricky part is to get the norm of the random 3D vector. The rest is about the 2 spherical angles θ (theta) and φ (phi), which is easy stuff. It turns out that in 3 dimensions, that norm involves the inverse of the incomplete gamma function. And if you have Python and Numpy/Scipy, this is function scipy.special.gammaincinv. We can thus write this code: import math import numpy.random as rd import scipy.special as sp # convert 3 uniform [0,1) variates into 3 unit Gaussian variates: def boxMuller3d(u3): u0,u1,u2 = u3 # 3 uniform random numbers in [0,1) gamma = u0 norm2 = 2.0 * sp.gammaincinv(1.5, gamma) # "regularized" versions norm = math.sqrt(norm2) zr = (2.0 * u1) - 1.0 # sin(theta) hr = math.sqrt(1.0 - zr*zr) # cos(theta) phi = 2.0 * math.pi * u2 xr = hr * math.cos(phi) yr = hr * math.sin(phi) g3 = list(map(lambda c: c*norm, [xr, yr, zr])) return g3 # generate 3 uniform variates and convert them into 3 unit Gaussian variates: def gauss3(rng): u3 = rng.uniform(0.0, 1.0, 3) g3 = boxMuller3d(u3) return g3 To (partly) check correctness, we can have this small main program, which displays the statistical moments of order 1 to 4 of the resulting random serie: randomSeed = 42 rng = rd.default_rng(randomSeed) count = 3000000 # (X,Y,Z) triplet count variates = [] for i in range(count): g3 = gauss3(rng) variates += g3 ln = len(variates) print("length=%d\n" % ln) # Checking statistical moments of order 1 to 4: m1 = sum(variates) / ln m2 = sum( map(lambda x: x*x, variates) ) / ln m3 = sum( map(lambda x: x**3, variates) ) / ln m4 = sum( map(lambda x: x**4, variates) ) / ln print("m1=%g m2=%g m3=%g m4=%g\n" % (m1,m2,m3,m4)) Test program output: length=9000000 m1=-0.000455911 m2=1.00025 m3=-0.000563454 m4=3.00184 We thus can see that these moments are reasonably close to their mathematically expected values, respectively 0,1,0,3.

Related questions

0 votes
    How can I request a random row (or as close to truly random as is possible) in pure SQL? Select the correct answer from above options...
asked Feb 8, 2022 in Education by JackTerrance
0 votes
    How can I request a random row (or as close to truly random as is possible) in pure SQL? Select the correct answer from above options...
asked Feb 5, 2022 in Education by JackTerrance
0 votes
    I have a list of objects in Python and I want to shuffle them. I thought I could use the random.shuffle method, ... (b) This will fail. Select the correct answer from above options...
asked Jan 27, 2022 in Education by JackTerrance
0 votes
    How can I generate a string of size N. I want it to be of uppercase English letters and numbers like this: 7I2D86 ... do it in Python. Select the correct answer from above options...
asked Jan 22, 2022 in Education by JackTerrance
0 votes
    I try to learn and implement a simple genetic algorithm library for my project. At this time, evolution, ... Gaussian distribution?.) Select the correct answer from above options...
asked Jan 26, 2022 in Education by JackTerrance
0 votes
    The Gaussian surface is (a) Real boundary (b) Imaginary surface (c) Tangential (d) Normal I ... proposed by,electromagnetic theory engineering physics,electromagnetic theory nptel...
asked Nov 11, 2021 in Education by JackTerrance
0 votes
    The Gaussian surface for a line charge will be (a) Sphere (b) Cylinder (c) Cube (d) ... theory proposed by,electromagnetic theory engineering physics,electromagnetic theory nptel...
asked Nov 11, 2021 in Education by JackTerrance
0 votes
    The Gaussian surface for a point charge will be (a) Cube (b) Cylinder (c) Sphere (d) ... theory proposed by,electromagnetic theory engineering physics,electromagnetic theory nptel...
asked Nov 11, 2021 in Education by JackTerrance
0 votes
    I have three 3D mesh matrices (X, Y, Z) corresponding to the xyz coordinate space. I also have a ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 19, 2022 in Education by JackTerrance
0 votes
    I need to load 3DsMax generated animation in DirectX.File type(.X or any..) is doesn't matters. ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 26, 2022 in Education by JackTerrance
0 votes
    _________ display contours of a 3d surface in 2d. (a) aes_contour (b) geom_contour (c) aes_gem (d) ... Analysis of R Programming Select the correct answer from above options...
asked Feb 13, 2022 in Education by JackTerrance
0 votes
    What is the difference between 1D, 2D and 3D convolutions in CNN? Please explain with examples. Select the correct answer from above options...
asked Jan 24, 2022 in Education by JackTerrance
0 votes
    Apparently, Microsoft has not yet made WebGL work in Internet Explorer by default. Plus, Opera have it only ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jan 15, 2022 in Education by JackTerrance
...