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

Pass RNG as first argument #4

Merged
merged 1 commit into from
Jan 16, 2019
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
30 changes: 15 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pois_rand(λ)
# Using another RNG
using RandomNumbers
rng = Xorshifts.Xoroshiro128Plus()
pois_rand(λ,rng)
pois_rand(rng)
```

## Implementation
Expand All @@ -29,46 +29,46 @@ using RandomNumbers, Distributions, BenchmarkTools, StaticArrays,
labels = ["count_rand","ad_rand","pois_rand","Distributions.jl"]
rng = Xorshifts.Xoroshiro128Plus()

function n_count(λ,rng,n)
function n_count(rng,n)
tmp = 0
for i in 1:n
tmp += PoissonRandom.count_rand(λ,rng)
tmp += PoissonRandom.count_rand(rng)
end
end

function n_pois(λ,rng,n)
function n_pois(rng,n)
tmp = 0
for i in 1:n
tmp += pois_rand(λ,rng)
tmp += pois_rand(rng)
end
end

function n_ad(λ,rng,n)
function n_ad(rng,n)
tmp = 0
for i in 1:n
tmp += PoissonRandom.ad_rand(λ,rng)
tmp += PoissonRandom.ad_rand(rng)
end
end

function n_dist(λ,rng,n)
function n_dist(λ,n)
tmp = 0
for i in 1:n
tmp += rand(Poisson(λ))
end
end

function time_λ(λ,rng,n)
t1 = @elapsed n_count(λ,rng,n)
t2 = @elapsed n_ad(λ,rng,n)
t3 = @elapsed n_pois(λ,rng,n)
t4 = @elapsed n_dist(λ,rng,n)
function time_λ(rng,n)
t1 = @elapsed n_count(rng,n)
t2 = @elapsed n_ad(rng,n)
t3 = @elapsed n_pois(rng,n)
t4 = @elapsed n_dist(λ,n)
@SArray [t1,t2,t3,t4]
end

# Compile
time_λ(5,rng,5000000)
time_λ(rng,5,5000000)
# Run with a bunch of λ
times = VectorOfArray([time_λ(n,rng,5000000) for n in 1:20])'
times = VectorOfArray([time_λ(rng,n,5000000) for n in 1:20])'
plot(times,labels = labels, lw = 3)
```

Expand Down
15 changes: 6 additions & 9 deletions src/PoissonRandom.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ using Random

export pois_rand

function count_rand(λ,rng::AbstractRNG=Random.GLOBAL_RNG)
count_rand(λ) = count_rand(Random.GLOBAL_RNG, λ)
function count_rand(rng::AbstractRNG, λ)
n = 0
c = randexp(rng)
while c < λ
Expand All @@ -22,7 +23,8 @@ end
#
# For μ sufficiently large, (i.e. >= 10.0)
#
function ad_rand(λ,rng::AbstractRNG=Random.GLOBAL_RNG)
ad_rand(λ) = ad_rand(Random.GLOBAL_RNG, λ)
function ad_rand(rng::AbstractRNG, λ)
s = sqrt(λ)
d = 6.0*λ^2
L = floor(Int,λ-1.1484)
Expand Down Expand Up @@ -139,12 +141,7 @@ function procf(λ, K::Int, s::Float64)
return px,py,fx,fy
end

function pois_rand(λ,rng::AbstractRNG=Random.GLOBAL_RNG)
if λ < 6
return count_rand(λ,rng)
else
return ad_rand(λ,rng)
end
end
pois_rand(λ) = pois_rand(Random.GLOBAL_RNG, λ)
pois_rand(rng::AbstractRNG, λ) = λ < 6 ? count_rand(rng, λ) : ad_rand(rng, λ)

end # module