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

add rand(::IntSet) #21960

Merged
merged 3 commits into from
May 30, 2017
Merged

add rand(::IntSet) #21960

merged 3 commits into from
May 30, 2017

Conversation

rfourquet
Copy link
Member

@rfourquet rfourquet commented May 19, 2017

The second commit optimizes rand(::Dict) by using a RangeGenerator object, the speed-up seems to be roughly +50%.

@rfourquet rfourquet added the domain:randomness Random number generation and the Random stdlib label May 19, 2017
@mbauman
Copy link
Sponsor Member

mbauman commented May 19, 2017

I'm not sure we're consistent enough about the max element of an IntSet for this to make sense. We've been treating it as an unbounded set.

@StefanKarpinski
Copy link
Sponsor Member

rand(::IntSet) should randomly select one of the values that's in the set and return it (if it doesn't do that, then we should change it). Why does that require having a defined upper bound on what could be put in an IntSet?

@rfourquet
Copy link
Member Author

I also don't understand what the problem is with the "max element"... There is a well defined number of elements in an IntSet, with a well defined set a contained integers, so selecting randomly one of them doesn't look like a consistency problem.

@mbauman
Copy link
Sponsor Member

mbauman commented May 19, 2017

My apologies, I didn't look closely enough. I was somehow thinking rand!. This is indeed very reasonable.

@rfourquet
Copy link
Member Author

Oh you remark make more sense now if you were thinking rand! !

@rfourquet
Copy link
Member Author

I realized that the non-scalar methods (creating/filling arrays) were missing for Dict, Set, IntSet, so I added them.

@rfourquet
Copy link
Member Author

Good to go?

base/random.jl Outdated
* a type: the set of values to pick from is then equivalent to `typemin(S):typemax(S)` for
integers (this is not applicable to `BigInt`), and to ``[0, 1)`` for floating point numbers;

`S` defaults to `Float64`.

```jldoctest
julia> srand(0); rand(Int, 2)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure whether srand(0) is needed here, for the doctests?

Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would think yes, since otherwise the results would be different every time.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I saw that it's also possible to use a julia-repl block instead of jldoctest. Could be cleaner...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Downside of that is that they wont be tested. On the other hand, these functions might be fundamental enough that they wont change. julia-repl is fine IMO.

Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rfourquet
Copy link
Member Author

Rebased. I will merge soon if no more comments come.

while true
n = rand(r, rg)
@inbounds b = s.bits[n]
b && return n
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will this actually be uniform?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe so, and this is how other uniform random functionality are implemented (rand on a range, on Dict, etc.). Basically pick uniformly one of the possible positions, and if it's not valid try again.

test/random.jl Outdated
a2 = rand(rng..., C, 2, 3) ::Array{T, 2}
a3 = rand!(rng..., Array{T}(5), C) ::Vector{T}
a4 = rand!(rng..., Array{T}(2, 3), C) ::Array{T, 2}
for a in [a0, a1..., a2...]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not also a3, a4?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, looks like unintentional omission.

1339893410598768192
1575814717733606317

julia> rand(MersenneTwister(0), Dict(1=>2, 3=>4))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is not a doctest perhaps the MersenneTwister(0) is unnecessary

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes right.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, I think I wanted to illustrate different combinations of arguments and show the use of an explicit RNG (and MersenneTwister is the most likely to be used, but doesn't accept for now no-argument), so I would rather keep it as is.

And change "dict" -> "collection" in the exception,
as it is also used from rand(::Set).
@rfourquet
Copy link
Member Author

Rebased. Will merge when CI turns green again.

@rfourquet rfourquet merged commit 086d36b into master May 30, 2017
@rfourquet rfourquet deleted the rf/rand-IntSet branch May 30, 2017 11:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
domain:randomness Random number generation and the Random stdlib
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants