Skip to content

Commit

Permalink
prepare RNG functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
schillic committed Nov 2, 2018
1 parent 1febd6d commit 7388c13
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 0 deletions.
1 change: 1 addition & 0 deletions Project_tmp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Expokit = "a1e7a1ef-7a5d-5822-a38c-be74e1bb89f4"
GLPKMathProgInterface = "3c7084bd-78ad-589a-b5bb-dbd673274bea"
IntervalArithmetic = "d1acc4aa-44c8-5952-acd4-ba5d80a2a253"
MathProgBase = "fdba3010-5040-5b88-9595-932c9decdf73"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
RecipesBase = "3cdcf5f2-1ef4-517c-9805-6587b60abb01"
Requires = "ae029012-a4dd-5104-9daa-d747884805df"
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
Expand Down
1 change: 1 addition & 0 deletions docs/src/lib/utils.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ an_element_helper
binary_search_constraints
nonzero_indices
samedir
reseed
```

### Types
Expand Down
8 changes: 8 additions & 0 deletions src/LazySet.jl
Original file line number Diff line number Diff line change
Expand Up @@ -239,3 +239,11 @@ function ==(X::LazySet, Y::LazySet)

return true
end

@static if VERSION >= v"0.7-"
# hook into random API
import Random.rand
function rand(rng::AbstractRNG, ::SamplerType{T}) where T<:LazySet
rand(T, rng=rng)
end
end
3 changes: 3 additions & 0 deletions src/compat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@ import Compat.InteractiveUtils.subtypes
export _At_mul_B

@static if VERSION < v"0.7-"
using Compat.Random
@inline function _At_mul_B(A, B)
return At_mul_B(A, B)
end
expmat = expm
else
using SparseArrays
using Random
using Random: GLOBAL_RNG, SamplerType
@inline function _At_mul_B(A, B)
return transpose(A) * B
end
Expand Down
25 changes: 25 additions & 0 deletions src/helper_functions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,31 @@ function nonzero_indices(v::SparseVector{N})::Vector{Int} where N<:Real
return x.nzind
end

"""
reseed(rng::AbstractRNG, seed::Union{Int, Nothing})::AbstractRNG
Reset the RNG seed if the seed argument is a number.
### Input
- `rng` -- random number generator
- `seed` -- seed for reseeding
### Output
The input RNG if the seed is `nothing`, and a reseeded RNG otherwise.
"""
function reseed(rng::AbstractRNG, seed::Union{Int, Nothing})::AbstractRNG
if seed != nothing
@static if VERSION < v"0.7-"
return Random.srand(rng, seed)
else
return Random.seed!(rng, seed)
end
end
return rng
end

"""
@neutral(SET, NEUT)
Expand Down
5 changes: 5 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ if test_suite_polyhedra || test_suite_plotting
end

if test_suite_basic
# =========================
# Testing utility functions
# =========================
@time @testset "LazySets.Util" begin include("unit_util.jl") end

# =======================================
# Testing types that inherit from LazySet
# =======================================
Expand Down
10 changes: 10 additions & 0 deletions test/unit_util.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
for _dummy_ in 1:1 # avoid global variable warnings
# reseeding with random seed
rng = LazySets.GLOBAL_RNG
seed = rand(1:10000)
LazySets.reseed(rng, seed)
n1 = rand(Int)
LazySets.reseed(rng, seed)
n2 = rand(Int)
@test n1 == n2
end

0 comments on commit 7388c13

Please sign in to comment.