From b1946607ee3a4d45312c344b7e6f681f8c4a447d Mon Sep 17 00:00:00 2001 From: islent Date: Mon, 22 Jan 2024 22:46:20 +0800 Subject: [PATCH] randin --- src/AstroSimBase.jl | 10 ++++++++++ test/runtests.jl | 6 ++++++ 2 files changed, 16 insertions(+) diff --git a/src/AstroSimBase.jl b/src/AstroSimBase.jl index 398bde1..5b98899 100644 --- a/src/AstroSimBase.jl +++ b/src/AstroSimBase.jl @@ -9,6 +9,7 @@ export TimeIntegration, Euler, Leapfrog, RK4 export BoundaryCondition, Periodic, Dirichlet, Vacuum, Newman, Reflective export traitstring, emptyfunction +export randin export mkpathIfNotExist export need_to_interrupt, interrupt @@ -97,6 +98,15 @@ function interrupt(OutputDir::String) close(f) end +""" +function randin(T, a, b) +function randin(a, b) + +Generate uniform random number in `[a,b]`. It avoids error from `rand(a:b)` where `a` and `b` are `Unitful.Quantity` +""" +randin(T, a, b) = T(rand(T) * (b-a) + a) +randin(a, b) = rand() * (b-a) + a + include("precompile.jl") end # module AstroSimBase \ No newline at end of file diff --git a/test/runtests.jl b/test/runtests.jl index 411f676..55ff2f7 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -43,4 +43,10 @@ end @test isfile("output/stop") sleep(0.2) @test !isfile("output/stop") + + r = randin(Float32,2,3) + @test 2<=r<=3 + + r = randin(2,3) + @test 2<=r<=3 end