Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rand(1:n) gives numbers outside 1:n #3004

Closed
fcoUnda opened this issue May 3, 2013 · 8 comments
Closed

rand(1:n) gives numbers outside 1:n #3004

fcoUnda opened this issue May 3, 2013 · 8 comments

Comments

@fcoUnda
Copy link

fcoUnda commented May 3, 2013

for i=1:100000
j = rand(1:7^7)
if(j<0) println(j) end
end

gives a bunch of negative numbers. I think there might be an abs missing in the last while of rand.

Cheers!

@lindahua
Copy link
Contributor

lindahua commented May 3, 2013

It seems to be working fine on my mac. I tried the following:

x = rand(1:7^7, 10^7)
all(x .> 0)   # outputs true

and

x = [rand(1:7^7) for i = 1 : 10^7]
all(x .> 0)   # outputs true

So, in both cases, no negative numbers were generated.

@fcoUnda
Copy link
Author

fcoUnda commented May 3, 2013

Both of these output false for me. I'm on a windows machine. Can anyone reproduce this?

@jiahao
Copy link
Member

jiahao commented May 3, 2013

What version are you running? I cannot reproduce this on osx with 2356fb8.

@quinnj
Copy link
Member

quinnj commented May 3, 2013

I can reproduce on the Windows binary, v0.1.2. I think it's related to #2587 which has been fixed in v0.2.

EDIT: I can't reproduce on my latest Windows 64-bit build (v0.2), just to confirm that it's been resolved.

@fcoUnda
Copy link
Author

fcoUnda commented May 3, 2013

Yes, I got my version from http:https://code.google.com/p/julialang/downloads/list, so its v0.1.2... my bad :P

@jiahao
Copy link
Member

jiahao commented May 3, 2013

Should be tagged for 0.1.3.

@GunnarFarneback
Copy link
Contributor

To reproduce on 64-bit Julia you need to explicitly make the range Int32.

And yes, there's clearly a bug there. The redrawing loop at the end also needs abs around the rand, except abs is fundamentally not the right operation in that function in the first place.

@GunnarFarneback
Copy link
Contributor

There are two reasons why abs is wrong. First it makes zero underrepresented compared to the non-zero values, skewing the distribution. Not necessarily a huge deal but to a large extent invalidating the redrawing loop at the end. Second, if you are unlucky enough to draw typemin of a signed type with at least as many bits as Int, abs won't change it and you end up with a negative number.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants