-
-
Notifications
You must be signed in to change notification settings - Fork 5.4k
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
Comments
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. |
Both of these output false for me. I'm on a windows machine. Can anyone reproduce this? |
What version are you running? I cannot reproduce this on osx with 2356fb8. |
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. |
Yes, I got my version from http:https://code.google.com/p/julialang/downloads/list, so its v0.1.2... my bad :P |
Should be tagged for 0.1.3. |
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. |
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. |
Repair rand{T<:Integer}(r::Range1{T}) for signed types. Closes #3004.
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!
The text was updated successfully, but these errors were encountered: