JuliaLang/julia — pull request #1035 by kmsquire (2012-07-11 01:41:27+00:00)
Title: Discrete rand update

(Apologies if multiple pull requests went to someone's inbox... I kept screwing up the commit range. This one should finally be correct.)

For discrete distributions, rand(d) produces Float64 samples:

```julia
julia> load("base/distributions.jl")

julia> b = Binomial()
Binomial(1,0.5)

julia> rand(b)
0.0

julia> rand(b)
1.0

julia> typeof(ans)
Float64
```

This is a result of calling libRmath, where all functions seem to return Float64. This patch casts the results of rand(d) for discrete distributions to integers, while leaving alone rand(d) for continuous distributions:

```julia

julia> load("base/distributions.jl")

julia> b = Binomial()
Binomial(1,0.5)

julia> rand(b)
0

julia> rand(b)
1

julia> g = Gaussian()
Normal(0.0,1.0)

julia> rand(g)
-0.4344590431025213
```

Eventually, these functions should probably be rewritten in Julia--at least, for most of the discrete distributions, it seems wasteful to covert back and forth from integers to floats.

For the patch, ```rand_type``` is set either to ```:int``` or ```""```, and then used to cast the function inside the macro. While it produces the correct results, I'm not exactly sure what happens when ```rand_type``` is ```""```, so feedback is appreciated.

Kevin