Skip to content

Commit 3970d79

Browse files
committed
Raise an error when seed! Xorshifts RNGs with 0. Fix #48.
1 parent 91a852b commit 3970d79

File tree

8 files changed

+15
-6
lines changed

8 files changed

+15
-6
lines changed

docs/src/man/xorshifts.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ julia> using RandomNumbers.Xorshifts
4242
4343
julia> r = Xoroshiro128Plus(); # create a RNG with truly random seed.
4444
45-
julia> r = Xoroshiro128Plus(0x1234567890abcdef) # with a certain seed.
45+
julia> r = Xoroshiro128Plus(0x1234567890abcdef) # with a certain seed. Note that the seed must be non-zero.
4646
RandomNumbers.Xorshifts.Xoroshiro128Plus(0x13c5b80b0f92f3ac, 0xcdd3f80fa9fb6887)
4747
4848
julia> rand(r)

src/Xorshifts/docs.jl

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ $r <: AbstractXorshift64
99
$r([seed])
1010
```
1111
12-
$r RNG. The `seed` will be automatically convert to an `UInt64` number.
12+
$r RNG. The `seed` will be automatically convert to an `UInt64` number. A zero seed is not acceptable.
1313
"""
1414
@doc doc_xorshift64("Xorshift64") Xorshift64
1515
@doc doc_xorshift64("Xorshift64Star") Xorshift64Star
@@ -22,7 +22,7 @@ $r([seed])
2222
```
2323
2424
$r RNG. The `seed` can be a `Tuple` of two `UInt64`s, or an `Integer` which will be automatically convert to
25-
an `UInt128` number.
25+
an `UInt128` number. Zero seeds are not acceptable.
2626
"""
2727
@doc doc_xorshift128("Xorshift128") Xorshift128
2828
@doc doc_xorshift128("Xorshift128Star") Xorshift128Star
@@ -36,7 +36,7 @@ $r([seed...])
3636
```
3737
3838
$r RNG. The `seed` can be a `Tuple` of 16 `UInt64`s, or several (no more than 16) `Integer`s which will all
39-
be automatically converted to `UInt64` numbers.
39+
be automatically converted to `UInt64` numbers. Zero seeds are not acceptable.
4040
"""
4141
@doc doc_xorshift1024("Xorshift1024") Xorshift1024
4242
@doc doc_xorshift1024("Xorshift1024Star") Xorshift1024Star
@@ -50,7 +50,7 @@ $r([seed])
5050
```
5151
5252
$r RNG. The `seed` can be a `Tuple` of two `UInt64`s, or an `Integer` which will be automatically convert to
53-
an `UInt128` number.
53+
an `UInt128` number. Zero seeds are not acceptable.
5454
"""
5555
@doc doc_xoroshiro128("Xoroshiro128") Xoroshiro128
5656
@doc doc_xoroshiro128("Xoroshiro128Star") Xoroshiro128Star

src/Xorshifts/xoroshiro128.jl

+1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ copy(src::T) where T <: AbstractXoroshiro128 = copyto!(T(), src)
5757

5858
seed!(r::AbstractXoroshiro128, seed::Integer) = seed!(r, split_uint(seed % UInt128))
5959
function seed!(r::AbstractXoroshiro128, seed::NTuple{2, UInt64}=gen_seed(UInt64, 2))
60+
all(==(0), seed) && error("0 cannot be the seed")
6061
r.x = seed[1]
6162
r.y = seed[2]
6263
xorshift_next(r)

src/Xorshifts/xorshift1024.jl

+1
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ copy(src::T) where T <: AbstractXorshift1024 = copyto!(T(), src)
8484
==(r1::T, r2::T) where T <: AbstractXorshift1024 = unsafe_compare(r1, r2, UInt64, 16) && r1.p == r2.p
8585

8686
function seed!(r::AbstractXorshift1024, seed::NTuple{16, UInt64})
87+
all(==(0), seed) && error("0 cannot be the seed")
8788
ptr = Ptr{UInt64}(pointer_from_objref(r))
8889
@inbounds for i in 1:16
8990
unsafe_store!(ptr, seed[i], i)

src/Xorshifts/xorshift128.jl

+1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ copy(src::T) where T <: AbstractXorshift128 = copyto!(T(), src)
5555
seed!(r::AbstractXorshift128, seed::Integer) = seed!(r, split_uint(seed % UInt128))
5656

5757
function seed!(r::AbstractXorshift128, seed::NTuple{2, UInt64}=gen_seed(UInt64, 2))
58+
all(==(0), seed) && error("0 cannot be the seed")
5859
r.x = seed[1]
5960
r.y = seed[2]
6061
xorshift_next(r)

src/Xorshifts/xorshift64.jl

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ copy(src::T) where T <: AbstractXorshift64 = copyto!(T(), src)
4646
==(r1::T, r2::T) where T <: AbstractXorshift64 = r1.x == r2.x
4747

4848
function seed!(r::AbstractXorshift64, seed::Integer=gen_seed(UInt64))
49+
seed == 0 && error("0 cannot be the seed")
4950
r.x = seed % UInt64
5051
xorshift_next(r)
5152
r

test/Xorshifts/runtests.jl

+5
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ for (rng_name, seed_t) in (
3636
redirect_stdout(outfile)
3737

3838
@eval x = $rng_name()
39+
@test_throws(
40+
ErrorException("0 cannot be the seed"),
41+
seed_t === NTuple{16, UInt64} ? seed!(x, zeros(UInt64, 16)...) : seed!(x, 0)
42+
)
3943
seed!(x)
4044
@test seed_type(x) == seed_t
4145
@test copyto!(copy(x), x) == x
@@ -54,5 +58,6 @@ for (rng_name, seed_t) in (
5458
end
5559
redirect_stdout(stdout_)
5660

61+
5762
compare_dirs("expected", "actual")
5863
cd(pwd_)

test/common.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using RandomNumbers
22
import Random: seed!
3-
using Test: @test
3+
using Test: @test, @test_throws
44
using Base.Printf: @printf
55

66
function compare_dirs(dir1::AbstractString, dir2::AbstractString)

0 commit comments

Comments
 (0)