Skip to content

Commit

Permalink
randin
Browse files Browse the repository at this point in the history
  • Loading branch information
islent committed Jan 22, 2024
1 parent d82d36e commit b194660
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/AstroSimBase.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
6 changes: 6 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit b194660

Please sign in to comment.