Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

faster randn!(::MersenneTwister, ::Array{Float64}) #35078

Merged
merged 2 commits into from
Apr 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@ Standard library changes

#### Random

* `randn!(::MersenneTwister, ::Array{Float64})` is faster, and as a result, for a given state of the RNG,
the corresponding generated numbers have changed ([#35078]).

#### REPL

Expand Down
2 changes: 1 addition & 1 deletion stdlib/LinearAlgebra/test/cholesky.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ end
n1 = div(n, 2)
n2 = 2*n1

Random.seed!(1234321)
Random.seed!(12343)

areal = randn(n,n)/2
aimg = randn(n,n)/2
Expand Down
2 changes: 1 addition & 1 deletion stdlib/LinearAlgebra/test/symmetric.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module TestSymmetric

using Test, LinearAlgebra, SparseArrays, Random

Random.seed!(101)
Random.seed!(1010)

@testset "Pauli σ-matrices: $σ" for σ in map(Hermitian,
Any[ [1 0; 0 1], [0 1; 1 0], [0 -im; im 0], [1 0; 0 -1] ])
Expand Down
28 changes: 24 additions & 4 deletions stdlib/Random/src/normal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@ julia> randn(rng, ComplexF32, (2, 3))
0.611224+1.56403im 0.355204-0.365563im 0.0905552+1.31012im
```
"""
@inline function randn(rng::AbstractRNG=default_rng())
@inline randn(rng::AbstractRNG=default_rng()) = _randn(rng, rand(rng, UInt52Raw()))

@inline function _randn(rng::AbstractRNG, r::UInt64)
@inbounds begin
r = rand(rng, UInt52())
r &= 0x000fffffffffffff
rabs = Int64(r>>1) # One bit for the sign
idx = rabs & 0xFF
x = ifelse(r % Bool, -rabs, rabs)*wi[idx+1]
Expand Down Expand Up @@ -95,9 +97,11 @@ julia> randexp(rng, 3, 3)
0.695867 0.693292 0.643644
```
"""
function randexp(rng::AbstractRNG=default_rng())
randexp(rng::AbstractRNG=default_rng()) = _randexp(rng, rand(rng, UInt52Raw()))

function _randexp(rng::AbstractRNG, ri::UInt64)
@inbounds begin
ri = rand(rng, UInt52())
ri &= 0x000fffffffffffff
idx = ri & 0xFF
x = ri*we[idx+1]
ri < ke[idx+1] && return x # 98.9% of the time we return here 1st try
Expand Down Expand Up @@ -162,6 +166,7 @@ function randexp! end

for randfun in [:randn, :randexp]
randfun! = Symbol(randfun, :!)
_randfun = Symbol(:_, randfun)
@eval begin
# scalars
$randfun(rng::AbstractRNG, T::BitFloatType) = convert(T, $randfun(rng))
Expand All @@ -175,6 +180,21 @@ for randfun in [:randn, :randexp]
A
end

# optimization for MersenneTwister, which randomizes natively Array{Float64}
function $randfun!(rng::MersenneTwister, A::Array{Float64})
if length(A) < 13
for i in eachindex(A)
@inbounds A[i] = $randfun(rng, Float64)
end
else
rand!(rng, A, CloseOpen12())
for i in eachindex(A)
@inbounds A[i] = $_randfun(rng, reinterpret(UInt64, A[i]))
end
end
A
end

$randfun!(A::AbstractArray) = $randfun!(default_rng(), A)

# generating arrays
Expand Down