diff --git a/Project.toml b/Project.toml index 70fc0cb..15a05b2 100644 --- a/Project.toml +++ b/Project.toml @@ -12,6 +12,7 @@ NiLangCore = "575d3204-02a4-11ea-3f62-238caa8bf11e" Reexport = "189a3867-3050-52da-a836-e630ba90ab69" SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf" TupleTools = "9d95972d-f1c8-5527-a6e0-b4b365fa01f6" +Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" [compat] FixedPointNumbers = "0.6, 0.7, 0.8" @@ -25,9 +26,8 @@ julia = "1.3,1.4" [extras] Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f" ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210" -Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" [targets] -test = ["Test", "Random", "Statistics", "Distributions", "ForwardDiff"] +test = ["Test", "Statistics", "Distributions", "ForwardDiff"] diff --git a/src/stdlib/rng.jl b/src/stdlib/rng.jl new file mode 100644 index 0000000..5719724 --- /dev/null +++ b/src/stdlib/rng.jl @@ -0,0 +1,41 @@ +using Random + +export LcgRNG + +struct LcgRNG{T<:Integer} <: AbstractRNG + a::T + c::T + m::T + x::T +end +LcgRNG(; seed=42) = LcgRNG( + UInt(6364136223846793005), + UInt(1442695040888963407), + UInt(1)< tget(2) + x_c += xnew - rng.c + anc1 += ainv * x_c + end + rng.x -= mod(anc1, rng.m) + ~@routine + SWAP(rng.x, xnew) + xnew → zero(T) +end \ No newline at end of file diff --git a/src/stdlib/stdlib.jl b/src/stdlib/stdlib.jl index a23210e..ae2fc4b 100644 --- a/src/stdlib/stdlib.jl +++ b/src/stdlib/stdlib.jl @@ -8,3 +8,4 @@ include("nnlib.jl") include("sparse.jl") include("mapreduce.jl") include("sorting.jl") +include("rng.jl") diff --git a/test/stdlib/rng.jl b/test/stdlib/rng.jl new file mode 100644 index 0000000..eef6c17 --- /dev/null +++ b/test/stdlib/rng.jl @@ -0,0 +1,10 @@ +using Test, NiLang, Random + +@testset "LcgRNG" begin + rng = LcgRNG(; seed=42) + @test rng isa LcgRNG{UInt64} + @test rng.x == 42 + @instr rand(rng) + @test rng.x != 42 + @test check_inv(rand, (rng,)) +end \ No newline at end of file diff --git a/test/stdlib/stdlib.jl b/test/stdlib/stdlib.jl index 7a8aa97..28d344a 100644 --- a/test/stdlib/stdlib.jl +++ b/test/stdlib/stdlib.jl @@ -5,3 +5,4 @@ include("statistics.jl") include("nnlib.jl") include("sparse.jl") include("mapreduce.jl") +include("rng.jl")