Random BigFloats Julia

Is it possible to generate random type numbers BigFloatuniformly distributed in the interval [0,1)?

I mean, because it is rand(BigFloat)not available, it seems to us that we should use BigFloat(rand())for this ending. However, this is unsatisfactory for me, since we generate random numbers Float64, which are converted BigFloatsmainly by "adding" a string of zeros, but in essence they are Float64random numbers. It's right? If so, is there a special library for generating random numbers with arbitrary precision?

+4
source share
3 answers

This answer is similar to @Dan Getz's but I more trust that it is uniform (to be honest, I did not do the chi ^ 2 test to check, or graphics, or anything else). @ Maybe I'm just not sure. This answer also takes into account the accuracy level of BigFloat that you can customize. (It is counter-intuitively possible that some BigFloatmay have less accuracy than usual Float64, etc.)

Base.rand(::Type{BigFloat}) =  get(tryparse(BigFloat, "0." .* join(rand(['0','1'], precision(BigFloat))), 2))

Example:

julia> rand(BigFloat)
5.775971375531676786209502831045802088939348666270278366043732289527176430673822e-02

julia> rand(BigFloat)
3.061194516436133765062723761241255852372302334681778915000436918803432772307184e-01

julia> rand(BigFloat)
     1.39772524343615633719535808470123032232713877796596771875414945796651457395665e-01

It is assumed that BigFloat has a known accuracy, so we must generate many random bits. Since we generate between [0,1), we know that the base-2 representation is 0.something. So, we generate the string in base-2 and analyze it in base 2.

+2
source

( Julia 0.7+) rand(BigFloat). https://github.com/JuliaLang/julia/pull/22720 (, , 0,6 MIT).

0.7-dev:

julia> rand(BigFloat)
5.381468355278042619008060591939155580805097923053455073547599255767645416051576e-01

julia> rand(BigFloat)
6.678413824545014328186230919629061895976571434256056298415613736582692882364622e-01

julia> rand(BigFloat)
1.388732949711447354224342960598222355426512649106497530016925083999303683268617e-01
+5

Is this method good:

randbigfloat(n) = 
  ( e = rand(0:n) ; 
    parse(BigFloat, join([rand('0':'9', e)...,'.',rand('0':'9', n-e)...])) )

Using:

julia> randbigfloat(10)
3.09424947699999999999999999999999999999999999999999999999999999999999999999999e+05

julia> randbigfloat(10)
7.146482599999999999999999999999999999999999999999999999999999999999999999999975

However, this may not be the most effective method.

+2
source

Source: https://habr.com/ru/post/1690223/


All Articles