@@ -43,40 +43,45 @@ The optional `T` argument specifies the element type, which defualts to `Float64
4343spzeros (dims:: Dims ) = spzeros (Float64, dims)
4444spzeros (:: Type{T} , dims:: Dims ) where {T} = SparseArrayDOK {T} (undef, dims)
4545
46+ @doc """
47+ sprand([rng], [T::Type], dims; density::Real=0.5, rfn::Function=rand) -> A::SparseArrayDOK{T}
48+
4649Create a random size `dims` sparse array in which the probability of any element being stored is independently given by `density`.
4750The optional `rng` argument specifies a random number generator, see also `Random`.
4851The optional `T` argument specifies the element type, which defaults to `Float64`.
4952
5053See also [`sprand!`](@ref).
5154""" sprand
5255
53- function sprand(::Type{T}, dims::Dims; density::Real=0.5 ) where {T}
54- return sprand(default_rng(), T, dims; density )
56+ function sprand (:: Type{T} , dims:: Dims ; kwargs ... ) where {T}
57+ return sprand (default_rng (), T, dims; kwargs ... )
5558end
56- sprand(dims::Dims; density::Real=0.5 ) = sprand(default_rng(), Float64, dims; density )
57- function sprand(rng::AbstractRNG, dims::Dims; density::Real=0.5 )
58- return sprand(rng, Float64, dims; density )
59+ sprand (dims:: Dims ; kwargs ... ) = sprand (default_rng (), Float64, dims; kwargs ... )
60+ function sprand (rng:: AbstractRNG , dims:: Dims ; kwargs ... )
61+ return sprand (rng, Float64, dims; kwargs ... )
5962end
60- function sprand(rng::AbstractRNG, ::Type{T}, dims::Dims; density::Real=0.5 ) where {T}
63+ function sprand (rng:: AbstractRNG , :: Type{T} , dims:: Dims ; kwargs ... ) where {T}
6164 A = SparseArrayDOK {T} (undef, dims)
62- sprand!(rng, A; density )
65+ sprand! (rng, A; kwargs ... )
6366 return A
6467end
6568
6669@doc """
67- sprand! ([rng], A:: AbstractArray ; density:: Real = 0.5 ) -> A
70+ sprand!([rng], A::AbstractArray; density::Real=0.5, rfn::Function=rand ) -> A
6871
6972Overwrite part of an array with random entries, where the probability of overwriting is independently given by `density`.
7073The optional `rng` argument specifies a random number generator, see also `Random`.
7174
7275See also [`sprand`](@ref).
7376""" sprand!
7477
75- sprand!(A::AbstractArray; density::Real=0.5) = sprand!(default_rng(), A; density)
76- function sprand!(rng::AbstractRNG, A::AbstractArray; density::Real=0.5)
78+ sprand! (A:: AbstractArray ; kwargs... ) = sprand! (default_rng (), A; kwargs... )
79+ function sprand! (
80+ rng:: AbstractRNG , A:: AbstractArray ; density:: Real = 0.5 , rfn:: Function = Random. rand
81+ )
82+ ArrayLayouts. zero! (A)
7783 rand_inds = Random. randsubseq (rng, eachindex (A), density)
78- rand_entries = rand(rng, eltype(A), length(rand_inds))
79-
84+ rand_entries = rfn (rng, eltype (A), length (rand_inds))
8085 for (I, v) in zip (rand_inds, rand_entries)
8186 A[I] = v
8287 end
0 commit comments