Skip to content

Commit 7ef6733

Browse files
committed
Add normally-distributed BigFloats generation
1 parent 3549bb8 commit 7ef6733

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

base/gmp.jl

+1
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,7 @@ function rand!(r::Range1{BigInt}, A::Array{BigInt})
475475
end
476476

477477
rand(r::Range1{BigInt}, dims::Dims) = rand!(r, Array(BigInt, dims))
478+
rand(r::Range1{BigInt}, dims::Int...) = rand!(r, Array(BigInt, dims...))
478479

479480
function randu(randstate::BigRNG, k::BigInt)
480481
z = BigInt()

base/mpfr.jl

+20-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import
1919
gamma, lgamma, digamma, erf, erfc, zeta, log1p, airyai, iceil, ifloor,
2020
itrunc, eps, signbit, sin, cos, tan, sec, csc, cot, acos, asin, atan,
2121
cosh, sinh, tanh, sech, csch, coth, acosh, asinh, atanh, atan2,
22-
serialize, deserialize, inf, nan, hash, cbrt, rand, rand!
22+
serialize, deserialize, inf, nan, hash, cbrt, rand, rand!, randn, randn!
2323

2424
import Base.Math.lgamma_r
2525

@@ -757,4 +757,23 @@ function rand!(r::BigRNG, A::Array{BigFloat})
757757
end
758758
rand!(A::Array{BigFloat}) = rand!(Base.GMP.DEFAULT_BIGRNG, A)
759759

760+
function randn(::Type{BigFloat}, randstate::BigRNG)
761+
z = BigFloat()
762+
ccall((:mpfr_grandom,:libmpfr), Int32,
763+
(Ptr{BigFloat}, Ptr{BigFloat}, Ptr{BigRNG}, Int32),
764+
&z, C_NULL, &randstate, ROUNDING_MODE[end])
765+
z
766+
end
767+
randn(::Type{BigFloat}) = randn(BigFloat, Base.GMP.DEFAULT_BIGRNG)
768+
randn(::Type{BigFloat}, dims::Dims) = randn!(Array(BigFloat, dims))
769+
randn(::Type{BigFloat}, dims::Int...) = randn!(Array(BigFloat, dims...))
770+
771+
function randn!(r::BigRNG, A::Array{BigFloat})
772+
for i = 1:length(A)
773+
A[i] = randn(BigFloat, r)
774+
end
775+
A
776+
end
777+
randn!(A::Array{BigFloat}) = randn!(Base.GMP.DEFAULT_BIGRNG, A)
778+
760779
end #module

0 commit comments

Comments
 (0)