In Julia 0.4.0, when I try
rand(AbstractFloat, 1)
The following error is obtained:
ERROR: MethodError: `rand` has no method matching rand(::MersenneTwister,
::Type{AbstractFloat})
Is there a reason behind the fact that I must explicitly say Float32
or Float64
for rand
to work? Or is it just that, as the language is relatively new, a relevant method has yet to be defined in the Base?
答案 0 :(得分:4)
one
与rand
不同。
使用one(AbstractFloat)
时,所有输出都是"相同":
julia> one(Float64)
1.0
julia> one(Float32)
1.0f0
julia> 1.0 == 1.0f0
true
使用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
这意味着如果rand
的行为类似one
,则可能会在两台不同的计算机上使用相同的种子获得两个不同的结果(例如,一个是x86,另一个是x64)。看看random.jl中的代码:
@inline rand {T<:Union {Bool,Int8,UInt8,Int16,UInt16,Int32,UInt32}}(r :: MersenneTwister,:: Type {T})= rand_ui52_raw(r)%T
rand(Signed)
& rand(Unsigned)
也是非法的。