From 67fb58af23989142ea486793dd3cc5ec77bc1a7f Mon Sep 17 00:00:00 2001 From: Joseph Perla Date: Tue, 11 Mar 2014 10:02:27 -0400 Subject: [PATCH] WIP: Add methods: optional RNG argument to rand([rng], Array{...}) and rand!([rng], Array{...}) that create arrays --- base/random.jl | 19 +++++++++++++++---- doc/stdlib/base.rst | 4 ++-- test/random.jl | 9 +++++++++ 3 files changed, 26 insertions(+), 6 deletions(-) diff --git a/base/random.jl b/base/random.jl index 199c178e2bf9b..f16a0ef630669 100644 --- a/base/random.jl +++ b/base/random.jl @@ -121,13 +121,14 @@ rand(::Type{Int128}) = int128(rand(Uint128)) rand!(A::Array{Float64}) = dsfmt_gv_fill_array_close_open!(A) rand(::Type{Float64}, dims::Dims) = rand!(Array(Float64, dims)) rand(::Type{Float64}, dims::Int...) = rand(Float64, dims) +rand!(rng::MersenneTwister, A::Array{Float64}) = dsfmt_fill_array_close_open!(rng.state, A) +rand(rng::AbstractRNG, ::Type{Float64}, dims::Dims) = rand!(rng, Array(Float64, dims)) +rand(rng::AbstractRNG, ::Type{Float64}, dims::Int...) = rand(rng, Float64, dims) rand(dims::Dims) = rand(Float64, dims) rand(dims::Int...) = rand(Float64, dims) - -rand!(r::MersenneTwister, A::Array{Float64}) = dsfmt_fill_array_close_open!(r.state, A) -rand(r::AbstractRNG, dims::Dims) = rand!(r, Array(Float64, dims)) -rand(r::AbstractRNG, dims::Int...) = rand(r, dims) +rand(rng::AbstractRNG, dims::Dims) = rand(rng, Float64, dims) +rand(rng::AbstractRNG, dims::Int...) = rand(rng, Float64, dims) function rand!{T}(A::Array{T}) for i=1:length(A) @@ -139,6 +140,16 @@ rand(T::Type, dims::Dims) = rand!(Array(T, dims)) rand{T<:Number}(::Type{T}) = error("no random number generator for type $T; try a more specific type") rand{T<:Number}(::Type{T}, dims::Int...) = rand(T, dims) +function rand!{T}(rng::AbstractRNG, A::Array{T}) + for i=1:length(A) + A[i] = rand(rng, T) + end + A +end +rand(rng::AbstractRNG, T::Type, dims::Dims) = rand!(rng, Array(T, dims)) +rand{T<:Number}(rng::AbstractRNG, ::Type{T}) = error("no random number generator for type $T; try a more specific type") +rand{T<:Number}(rng::AbstractRNG, ::Type{T}, dims::Int...) = rand(rng, T, dims) + # Generate random integer within a range immutable RandIntGen{T<:Integer, U<:Unsigned} diff --git a/doc/stdlib/base.rst b/doc/stdlib/base.rst index bfe4fe1d7b2c9..bde8031f9d39b 100644 --- a/doc/stdlib/base.rst +++ b/doc/stdlib/base.rst @@ -3411,11 +3411,11 @@ Random number generation in Julia uses the `Mersenne Twister library