rand(x::Int32) doesn't exist, omission or intentional?

196 views
Skip to first unread message

Jeff Waller

unread,
Aug 10, 2014, 7:14:08 PM8/10/14
to julia...@googlegroups.com
Seems like the intention is to cover many possibilities.  This would be the typical rand but with type Int32 not Int64  for example.

julia> rand(2)

2-element Array{Float64,1}:

 0.690068

 0.137219

julia> rand(int32(2))

ERROR: `rand` has no method matching rand(::Int32)

Is there a reason for this just kind of got overlooked because the usage is rare?

Clemens Heitzinger

unread,
Aug 11, 2014, 9:04:23 AM8/11/14
to julia...@googlegroups.com
Works like this (on 0.3.0-rc3):

julia> methods(rand)

# 31 methods for generic function "rand":

rand(::Type{Float64}) at random.jl:88

rand() at random.jl:89

rand(::Type{Float32}) at random.jl:91

rand(::Type{Float16}) at random.jl:92

rand{T<:Real}(::Type{Complex{T<:Real}}) at random.jl:94

rand(r::MersenneTwister) at random.jl:97

rand(::Type{Uint8}) at random.jl:104

rand(::Type{Uint16}) at random.jl:105

rand(::Type{Uint32}) at random.jl:106

rand(::Type{Uint64}) at random.jl:107

rand(::Type{Uint128}) at random.jl:108

rand(::Type{Int8}) at random.jl:110

rand(::Type{Int16}) at random.jl:111

rand(::Type{Int32}) at random.jl:112

rand(::Type{Int64}) at random.jl:113

rand(::Type{Int128}) at random.jl:114

rand(::Type{Float64},dims::(Int64...,)) at random.jl:118

rand(::Type{Float64},dims::Int64...) at random.jl:119

rand(dims::(Int64...,)) at random.jl:121

rand(dims::Int64...) at random.jl:122

rand(r::AbstractRNG,dims::(Int64...,)) at random.jl:124

rand(r::AbstractRNG,dims::Int64...) at random.jl:125

rand{T<:Number}(::Type{T<:Number}) at random.jl:142

rand(T::Type{T<:Top},dims::(Int64...,)) at random.jl:141

rand{T<:Number}(::Type{T<:Number},dims::Int64...) at random.jl:143

rand{T<:Union(Int64,Uint64)}(g::RandIntGen{T<:Union(Int64,Uint64),Uint64}) at random.jl:185

rand{T<:Integer,U<:Unsigned}(g::RandIntGen{T<:Integer,U<:Unsigned}) at random.jl:201

rand{T<:Union(Bool,Unsigned,Char,Signed)}(r::UnitRange{T<:Union(Bool,Unsigned,Char,Signed)}) at random.jl:208

rand{T}(r::Range{T}) at random.jl:209

rand{T}(r::Range{T},dims::(Int64...,)) at random.jl:236

rand(r::Range{T},dims::Int64...) at random.jl:237


julia> rand(Int32)

-1091619314


julia> rand(Int32, 10)

10-element Array{Int32,1}:

  -232135574

 -1289647599

 -1136027271

   575399837

  -761829090

 -1955750348

 -2129358352

  -554861937

  -934832989

 -1179801152

Steven G. Johnson

unread,
Aug 11, 2014, 12:29:27 PM8/11/14
to julia...@googlegroups.com


On Sunday, August 10, 2014 7:14:08 PM UTC-4, Jeff Waller wrote: 

Is there a reason for this just kind of got overlooked because the usage is rare?

I think it just got overlooked.  Because ultimately it gets converted to an (Int...) tuple, it is easy to be lazy and just require that the arguments be Int's in functions like rand.

Steven G. Johnson

unread,
Aug 11, 2014, 1:34:05 PM8/11/14
to julia...@googlegroups.com
On Monday, August 11, 2014 9:04:23 AM UTC-4, Clemens Heitzinger wrote:
Works like this (on 0.3.0-rc3):

Clemens, I think Jeff wanted to generate a random Float64 array from an int32 size, not an array of random Int32 values.

I added a pull request (https://github.com/JuliaLang/julia/pull/7956) that fixes this.

Jeff Waller

unread,
Aug 11, 2014, 7:31:16 PM8/11/14
to julia...@googlegroups.com
Excellent, I had one too just in case. I also tried changing Int... to Integer... but that had problems because it needs to convert to a Dim  I ended up doing essentially an Int32... to dim via convert.  Maybe that's too inefficient...

e.g.  @approximately line 120

rand(::Type{Float64}, dims::Int32...) = rand(Float64,convert(Dims,dims))

Reply all
Reply to author
Forward
0 new messages