Rename random(List) to shuffle(List) so that random(List) returns a single element#4224
Merged
Merged
Conversation
64d630d to
9482622
Compare
mahrud
reviewed
Apr 26, 2026
mahrud
reviewed
Apr 26, 2026
mahrud
reviewed
Apr 26, 2026
mahrud
reviewed
Apr 26, 2026
mahrud
reviewed
Apr 26, 2026
mahrud
reviewed
Apr 26, 2026
mahrud
reviewed
Apr 26, 2026
0f51acf to
30b8d6e
Compare
mahrud
reviewed
Apr 26, 2026
23d721f to
a47a5e6
Compare
Now random(List) returns a random element of the list
Or sometimes randomSubset
Member
Author
|
I've implemented the suggestions from this afternoon's M2internals: i1 : x = toList(0..12)
o1 = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}
o1 : List
i2 : random x
-- the behavior of random(List) will change soon; use shuffle(List) instead
o2 = {0, 7, 9, 4, 5, 3, 8, 12, 10, 11, 6, 2, 1}
o2 : List
i3 : random x
o3 = {5, 9, 1, 7, 12, 8, 10, 0, 6, 3, 4, 11, 2}
o3 : List
i4 : randomElement x
o4 = 0
i5 : shuffle x
o5 = {1, 4, 0, 9, 8, 3, 2, 5, 7, 10, 11, 6, 12}
o5 : List |
6abb8f1 to
2c83355
Compare
Now, random(List) prints a one-time warning and then calls shuffle(List).
Eventually -> random(List)
mahrud
reviewed
May 1, 2026
Comment on lines
+232
to
+234
| randomElement List := x -> ( | ||
| if #x == 0 then error "expected a nonempty list"; | ||
| x#(random(#x))) |
Member
There was a problem hiding this comment.
Not important for this PR, but related to #4230, I've noticed that something like the following is sometimes a little faster. I'd like to understand which interpreter optimizations come into play here better.
Suggested change
| randomElement List := x -> ( | |
| if #x == 0 then error "expected a nonempty list"; | |
| x#(random(#x))) | |
| randomElement List := x -> if #x > 0 then x#(random(#x)) else error "expected a nonempty list" |
(I forget, it's possible the then and else clauses need to be swapped for best performance)
Member
Author
|
Discussed with Mike |
Member
|
Did you check if there are any |
Member
Author
|
There shouldn't be. In earlier commits prior to the M2internals discussion, I'd changed the behavior of |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This addresses #4161.
I've also found the current behavior confusing. Broadly speaking, the other
randommethods take a "set" and return a random element of that set (whether that set is a ring, a number representing an upper bound of an interval, or what have you -- it's the same basic idea). Butrandom(List)doesn't do that.So we change its behavior so that it does, and introduce
shuffle(List)to take on the previous behavior.This was a pretty big change and I had to fix a bunch of packages that this change broke. Maybe we can discuss at the next M2internals for a consensus on whether it would be worth merging?