Julia: Feature requested: Randomly generate float in a range

Created on 24 Jun 2017  Â·  8Comments  Â·  Source: JuliaLang/julia

Right now we have the rand() function to randomly generate a float between 0 and 1. I would like to create a matrix of float between -1 and 1, conveniently, meaning I would like to do it one line as usual by providing arguments to the normal rand() function. I tried feeding rand() -1:1 along with my dim, yet it treats my range as a two-element list instead of a float range.

Most helpful comment

Pkg.add("Distributions")
import Distributions: Uniform 
rand(Uniform(-1,1), 3, 3)

All 8 comments

julia> s = 2; A = s*rand(3, 3)-s/2
3×3 Array{Float64,2}:
 0.286097  -0.619115   0.841564
 0.505293   0.237119  -0.656106
 0.823477   0.118682   0.663241

@fredrikekre wow.... genius

Still could be nice if we were to be provided a range argument in rand() tho!

Pkg.add("Distributions")
import Distributions: Uniform 
rand(Uniform(-1,1), 3, 3)

We should say whether it is random, or pseudo-random - right now it won't
make a difference, but will soon.

On Sat, Jun 24, 2017 at 2:45 AM, Kevin Squire notifications@github.com
wrote:

Pkg.add("Distributions")import Distributions: Uniform rand(Uniform(-1,1), 3, 3)

—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/JuliaLang/julia/issues/22502#issuecomment-310820090,
or mute the thread
https://github.com/notifications/unsubscribe-auth/ATKyIq-dGxHwokd5m2Hlt0iKHHNpdRweks5sHLCYgaJpZM4OEMSe
.

@fufjvnvnf Note that a float range is not the same as an interval, contrary to what happens with integers. So rand(1.0:5.0) would have to be equivalent to Float64.(rand(1:5)), which probably isn't very useful, and could even be misleading.

@kmsquire can I do the same too for Normal?

Please post questions to the Julia discourse discussion forum.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

felixrehren picture felixrehren  Â·  3Comments

wilburtownsend picture wilburtownsend  Â·  3Comments

Keno picture Keno  Â·  3Comments

ararslan picture ararslan  Â·  3Comments

i-apellaniz picture i-apellaniz  Â·  3Comments