-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
randn() and BigFloat #27858
Comments
@AshtonSBradley thanks for opening this. One thing to note though is that we use a lot of RandomNumbers.jl for the application here, so if this gets into v0.7 we can modify the default for the BigFloat case but I hope that whatever solution is found can be downstreamed to some of RandomNumbers.jl as well. |
I don't think we have ever supported |
Yes, the introduction of the I'm in the process of re-rewriting this doc, so will clarify this point while I'm at it, thanks. Chris, I don't really understand what you said, but if this ever gets implemented in |
This isn't just a documentation bug. This was noticed by Ashton when trying to solve stochastic differential equations with BigFloats, and instead we're trying to find a way to get random normal BigFloats. While there does seem to be a documentation bug here (it saying it exists when it doesn't), the real underlying thing is a feature request for random BigFloats.
Part of that response was continuing the conversation which led to this issue, sorry. I'm not sure you can map a single BigFloat handling to all PRNG algorithms? If you take the current RNGs can you just upscale them to larger digit sizes? For example, for a 256 BigFloat, just take 8 64-bit Floats from the same RNG to build a uniform distribution RNG and use that in the normal distribution? I am not very familiar with all of the RNG stuff, so maybe it is that simple and I just don't know. |
That's a dup of #17629. |
Maybe not so easily, but this is on the radar to have an API allowing RNGs implementors to plug into already implemented random generation algorithms. For example it's already quite easy to define an RNG and have it generate |
I thought that the details of the stuff inside the ziggurat are hardcoded, and that it would be different for different precisions. |
There are various non-Ziggurat algorithms, some of which are cited in the other issue. |
The documentation is pretty clear about which number types are supported for |
For anybody like me who landed here while Googling for a workaround: the following hack works if you just want your codebase to be generic to using Random: AbstractRNG
try
# In case randn(BigFloat) gets defined in the future
randn(BigFloat)
catch e
if isa(e, MethodError)
Base.randn(::Type{BigFloat}, args::Integer...) = BigFloat.(randn(Float64, args...))
Base.randn(rng::AbstractRNG, ::Type{BigFloat}, args::Integer...) = BigFloat.(randn(rng, Float64, args...))
else
throw(e)
end
end If you do want a high-precision RNG and don't need it to be fast you could use using Random: AbstractRNG
using SpecialFunctions: erfinv
try
# In case randn(BigFloat) gets defined in the future
randn(BigFloat)
catch e
if isa(e, MethodError)
gauss_q(p::T) where {T} = sqrt(T(2))*erfinv(2*p-1)
Base.randn(::Type{BigFloat}, args::Integer...) = gauss_q.(rand(BigFloat, args...))
Base.randn(rng::AbstractRNG, ::Type{BigFloat}, args::Integer...) = gauss_q.(rand(rng, BigFloat, args...))
else
throw(e)
end
end |
Note that in 1.9, |
Nice! Thanks 😅 |
In the docs:
https://docs.julialang.org/en/latest/stdlib/Random/
However,
Is there some special syntax to call randn() for BigFloat? Is it supported?
The text was updated successfully, but these errors were encountered: