Skip to content

Commit 0c6b81a

Browse files
committed
unify random array filling methods via Distribution
1 parent cedf597 commit 0c6b81a

File tree

1 file changed

+17
-23
lines changed

1 file changed

+17
-23
lines changed

base/random.jl

+17-23
Original file line numberDiff line numberDiff line change
@@ -216,9 +216,14 @@ rand(r::AbstractRNG, T::Type, d1::Integer, dims::Integer...) = rand(r, T, tuple(
216216
# rand(r, ()) would match both this method and rand(r, dims::Dims)
217217
# moreover, a call like rand(r, NotImplementedType()) would be an infinite loop
218218

219-
function rand!{T}(r::AbstractRNG, A::AbstractArray{T})
219+
abstract Distribution
220+
type TypeDistribution{T} <: Distribution end
221+
222+
@inline rand{T}(r::AbstractRNG, ::TypeDistribution{T}) = rand(r, T)
223+
224+
function rand!{T}(r::AbstractRNG, A::AbstractArray{T}, d::Distribution=TypeDistribution{T}())
220225
for i = 1:length(A)
221-
@inbounds A[i] = rand(r, T)
226+
@inbounds A[i] = rand(r, d)
222227
end
223228
A
224229
end
@@ -479,7 +484,7 @@ end
479484

480485
# rand on AbstractArray
481486

482-
immutable Sampler{A<:AbstractArray,G<:RangeGenerator}
487+
immutable Sampler{A<:AbstractArray,G<:RangeGenerator} <: Distribution
483488
r::A
484489
g::G
485490
end
@@ -488,13 +493,6 @@ end
488493
@inline rand(rng::AbstractRNG, s::Sampler) = @inbounds return s.r[rand(rng, s.g)]
489494
@inline rand{T<:Union(IntTypes..., BigInt)}(rng::AbstractRNG, s::Sampler{UnitRange{T}}) = rand(rng, s.g, first(s.r))
490495

491-
function rand!(rng::AbstractRNG, A::AbstractArray, s::Sampler)
492-
for i = 1 : length(A)
493-
@inbounds A[i] = rand(rng, s)
494-
end
495-
return A
496-
end
497-
498496

499497
# Randomly draw a sample from an AbstractArray r
500498
# (e.g. r is a range 0:2:8 or a vector [2, 3, 5, 7])
@@ -1012,6 +1010,12 @@ const ziggurat_nor_inv_r = inv(ziggurat_nor_r)
10121010
const ziggurat_exp_r = 7.6971174701310497140446280481
10131011

10141012

1013+
type Normal <: Distribution end
1014+
type Exponential <: Distribution end
1015+
1016+
@inline rand(rng::AbstractRNG, ::Normal) = randn(rng)
1017+
@inline rand(rng::AbstractRNG, ::Exponential) = randexp(rng)
1018+
10151019
@inline function randn(rng::AbstractRNG=GLOBAL_RNG)
10161020
@inbounds begin
10171021
r = rand_ui52(rng)
@@ -1038,19 +1042,14 @@ function randn_unlikely(rng, idx, rabs, x)
10381042
end
10391043
end
10401044

1041-
function randn!(rng::AbstractRNG, A::Array{Float64})
1042-
for i = 1:length(A)
1043-
@inbounds A[i] = randn(rng)
1044-
end
1045-
A
1046-
end
1047-
1045+
randn!(rng::AbstractRNG, A::Array{Float64}) = rand!(rng, A, Normal())
10481046
randn!(A::Array{Float64}) = randn!(GLOBAL_RNG, A)
10491047
randn(dims::Dims) = randn!(Array(Float64, dims))
10501048
randn(dims::Integer...) = randn!(Array(Float64, dims...))
10511049
randn(rng::AbstractRNG, dims::Dims) = randn!(rng, Array(Float64, dims))
10521050
randn(rng::AbstractRNG, dims::Integer...) = randn!(rng, Array(Float64, dims...))
10531051

1052+
10541053
@inline function randexp(rng::AbstractRNG=GLOBAL_RNG)
10551054
@inbounds begin
10561055
ri = rand_ui52(rng)
@@ -1071,13 +1070,8 @@ function randexp_unlikely(rng, idx, x)
10711070
end
10721071
end
10731072

1074-
function randexp!(rng::AbstractRNG, A::Array{Float64})
1075-
for i = 1:length(A)
1076-
@inbounds A[i] = randexp(rng)
1077-
end
1078-
A
1079-
end
10801073

1074+
randexp!(rng::AbstractRNG, A::Array{Float64}) = rand!(rng, A, Exponential())
10811075
randexp!(A::Array{Float64}) = randexp!(GLOBAL_RNG, A)
10821076
randexp(dims::Dims) = randexp!(Array(Float64, dims))
10831077
randexp(dims::Int...) = randexp!(Array(Float64, dims))

0 commit comments

Comments
 (0)