Skip to content

Commit

Permalink
Make Random for a (finite) collection take an optional source
Browse files Browse the repository at this point in the history
This requires moving the function as it was defined too early
to use InstallMethodWithRandomSource
  • Loading branch information
ChrisJefferson committed Mar 1, 2018
1 parent 38cb43a commit 468c7b9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
11 changes: 8 additions & 3 deletions lib/coll.gi
Original file line number Diff line number Diff line change
Expand Up @@ -264,12 +264,17 @@ InstallGlobalFunction( RandomList, function(args...)

return list[Random(source, 1, Length(list))];
end );
InstallMethod( Random, "for a (finite) collection",
[ IsCollection and IsFinite ],

# Use -SUM_FLAGS, as this should always be the last choice method.
# In particular, we want Random(SomeRandomSource, IsList) to come higher
# for lists, to avoid an infinite loop.
InstallMethodWithRandomSource( Random, "for a (finite) collection",
[ IsCollection and IsFinite ], -SUM_FLAGS,
C -> RandomList( Enumerator( C ) ) );


RedispatchOnCondition(Random,true,[IsCollection],[IsFinite],0);

RedispatchOnCondition(Random,true,[IsRandomSource,IsCollection],[IsFinite],0);

#############################################################################
##
Expand Down
10 changes: 10 additions & 0 deletions lib/random.gi
Original file line number Diff line number Diff line change
Expand Up @@ -311,3 +311,13 @@ end );
InstallGlobalFunction("InstallMethodWithRandomSource", func(InstallMethod));
InstallGlobalFunction("InstallOtherMethodWithRandomSource", func(InstallOtherMethod));
end)();

InstallMethod( Random, "fallback for GlobalMersenneTwister",
[IsMersenneTwister, IsCollection],
function(rs, o)
if rs = GlobalMersenneTwister then
return Random(o);
else
TryNextMethod();
fi;
end);

0 comments on commit 468c7b9

Please sign in to comment.