Random Numbers with Gaussian and Uniform Distributions in matlab

前端 未结 6 1812
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-09 07:12

I want generate a number in Gaussian and Uniform distributions in matlab. I know this function randi and rand() but all of them are in normal (Gau

6条回答
  •  暖寄归人
    2020-12-09 07:27

    Following raj's answer: by using the Box-Muller Transform you can generate independent standard Normal/Gaussian random numbers:

    N = 1e6; z = sqrt(-2*log(rand(N, 1))) .* cos(2*pi * rand(N, 1)); figure; hist(z, 100)
    N = 1e6; z = sqrt(-2*log(rand(N, 1))) .* sin(2*pi * rand(N, 1)); figure; hist(z, 100)
    

    If you want to apply the Inverse Transformation Method, you can use the Inverse Complementary Error Function (erfcinv):

    N = 1e6; z = -sqrt(2) * erfcinv(2 * rand(1e6, 1)); figure; hist(z, 100)
    

    But I hope randn works better.

提交回复
热议问题