Skip to content

Rename random(List) to shuffle(List) so that random(List) returns a single element#4224

Merged
d-torrance merged 7 commits into
Macaulay2:developmentfrom
d-torrance:shuffle
May 5, 2026
Merged

Rename random(List) to shuffle(List) so that random(List) returns a single element#4224
d-torrance merged 7 commits into
Macaulay2:developmentfrom
d-torrance:shuffle

Conversation

@d-torrance
Copy link
Copy Markdown
Member

This addresses #4161.

I've also found the current behavior confusing. Broadly speaking, the other random methods 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). But random(List) doesn't do that.

So we change its behavior so that it does, and introduce shuffle(List) to take on the previous behavior.

i1 : x = toList(1..20)

o1 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}

o1 : List

i2 : random x

o2 = 6

i3 : shuffle x

o3 = {11, 10, 18, 12, 16, 20, 7, 15, 6, 14, 4, 9, 17, 8, 13, 1, 2, 19, 3, 5}

o3 : List

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?

@d-torrance d-torrance force-pushed the shuffle branch 3 times, most recently from 64d630d to 9482622 Compare April 26, 2026 02:04
Comment thread M2/Macaulay2/packages/PathSignatures/algebra.m2
Comment thread M2/Macaulay2/packages/SchurComplexes.m2 Outdated
Comment thread M2/Macaulay2/packages/SchurFunctors.m2 Outdated
Comment thread M2/Macaulay2/packages/RationalPoints2.m2 Outdated
Comment thread M2/Macaulay2/packages/PieriMaps.m2 Outdated
Comment thread M2/Macaulay2/packages/GKMVarieties/Tests_GKMVarieties.m2 Outdated
Comment thread M2/Macaulay2/packages/CodingTheory.m2 Outdated
@d-torrance d-torrance force-pushed the shuffle branch 7 times, most recently from 0f51acf to 30b8d6e Compare April 26, 2026 20:39
Comment thread M2/Macaulay2/m2/lists.m2
@d-torrance d-torrance force-pushed the shuffle branch 3 times, most recently from 23d721f to a47a5e6 Compare April 27, 2026 02:33
@d-torrance
Copy link
Copy Markdown
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

@d-torrance d-torrance force-pushed the shuffle branch 2 times, most recently from 6abb8f1 to 2c83355 Compare May 1, 2026 00:50
Eventually -> random(List)
Comment thread M2/Macaulay2/m2/lists.m2
Comment on lines +232 to +234
randomElement List := x -> (
if #x == 0 then error "expected a nonempty list";
x#(random(#x)))
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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)

@d-torrance d-torrance marked this pull request as ready for review May 1, 2026 18:57
@d-torrance
Copy link
Copy Markdown
Member Author

Discussed with Mike

@d-torrance d-torrance merged commit fb0d307 into Macaulay2:development May 5, 2026
5 checks passed
@mahrud
Copy link
Copy Markdown
Member

mahrud commented May 5, 2026

Did you check if there are any the behavior of... strings printed in the package examples?

@d-torrance
Copy link
Copy Markdown
Member Author

There shouldn't be. In earlier commits prior to the M2internals discussion, I'd changed the behavior of random(List) already, so everything that called it has been changed to shuffle or randomSubset so the builds would pass.

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

Successfully merging this pull request may close these issues.

2 participants