Why doesn't rand work with AbstractFloat?

In Julia 0.4.0, when I try

rand(AbstractFloat, 1) 

Received the following error:

 ERROR: MethodError: `rand` has no method matching rand(::MersenneTwister, ::Type{AbstractFloat}) 

Is there a reason that I have to explicitly tell Float32 or Float64 for rand to work? Or simply because, since the language is relatively new, the corresponding method has not yet been defined in the database?

+6
source share
1 answer

one is different from rand . when using one(AbstractFloat) all outputs are "the same":

 julia> one(Float64) 1.0 julia> one(Float32) 1.0f0 julia> 1.0 == 1.0f0 true 

this is not true when using rand :

 julia> rand(srand(1), Float64) 0.23603334566204692 julia> rand(srand(1), Float32) 0.5479944f0 julia> rand(srand(1), Float32) == rand(srand(1), Float64) false 

this means that if rand behaves like one , you can get two different results with the same seed on two different machines (for example, one is x86, the other is x64). take a look at the code in random.jl :

@inline rand {T <: Union {Bool, Int8, UInt8, Int16, UInt16, Int32, UInt32}} (r :: MersenneTwister, :: Type {T}) = rand_ui52_raw (r)% T

both rand(Signed) & rand(Unsigned) are also illegal.

+4
source

All Articles