Skip to content
Open
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
3 changes: 2 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
name = "StableRNGs"
uuid = "860ef19b-820b-49d6-a774-d7a799459cd3"
authors = ["Rafael Fourquet <fourquet.rafael@gmail.com>"]
version = "1.0.0"
version = "1.1.0"

[deps]
Future = "9fa8497b-333b-5362-9e8d-4d0656e87820"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

Expand Down
12 changes: 11 additions & 1 deletion src/StableRNGs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export StableRNG
using Random: Random, AbstractRNG, Sampler, SamplerType

import Random: rand, seed!

import Future: randjump

# implementation of LehmerRNG based on the constants found at the
# MIT licensed code by Melissa E. O'Neill at
Expand Down Expand Up @@ -132,5 +132,15 @@ for T in Base.BitInteger_types
SamplerRangeFast(r)
end

"""
randjump(r::LehmerRNG, steps::Integer=1<<20) -> LehmerRNG


Create an initialized `LehmerRNG` object, whose state is moved forward (without generating numbers) from r by
`steps` steps. One such step corresponds to the generation of one `Float64` number.
"""
function randjump(rng::LehmerRNG, steps::Base.BitInteger = 1<<20)
StableRNG(state = rng.state * (0x45a31efc5a35d971261fd0407a968add^steps))
end

end # module
11 changes: 11 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,14 @@ end
shuffle!(StableRNG(10), b)
@test b == a_shuffled
end

using Future: randjump
@testset "`randjump`" begin
r = StableRNG(0)
r1 = randjump(r, 1)
r1234 = randjump(r, 1234)
rand(r)
@test r == r1
for _ = 1:1233; rand(r); end
@test r == r1234
end