Skip to content

Commit

Permalink
randn
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffreySarnoff authored May 14, 2019
1 parent b7a6bed commit 14d9131
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/extras/random.jl
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,22 @@ function randpm(rng::MersenneTwister, ::Type{Complex{DoubleFloat{T}}}, n::Int) w
ims = ims .* isgns
return map((re,im)->Complex{DoubleFloat{T}}(re,im), res, ims)
end

# normal variates

function randn(rng::AbstractRNG, ::Type{DoubleFloat{T}}) where {T<:IEEEFloat}
urand1, urand2 = rand(DoubleFloat{T}, 2)
urand1 = urand1 + urand1 - 1
urand2 = urand2 + urand2 - 1
s = urand1*urand1 + urand2*urand2

while s >= 1 || s === 0
urand1, urand2 = rand(DoubleFloat{T}, 2)
urand1 = urand1 + urand1 - 1
urand2 = urand2 + urand2 - 1
s = urand1*urand1 + urand2*urand2
end

s = sqrt( -log(s) / s )
return (urand1 + urand2) * s
end

0 comments on commit 14d9131

Please sign in to comment.