Skip to content

Commit

Permalink
Add an optional random source to RandomList
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisJefferson committed Mar 6, 2018
1 parent 3859d75 commit 40bc9ac
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 6 deletions.
6 changes: 4 additions & 2 deletions hpcgap/lib/coll.gd
Original file line number Diff line number Diff line change
Expand Up @@ -1661,15 +1661,17 @@ DeclareOperation( "Random", [ IS_INT, IS_INT ] );
##
## <#GAPDoc Label="RandomList">
## <ManSection>
## <Func Name="RandomList" Arg='list'/>
## <Func Name="RandomList" Arg='[rs,] list'/>
##
## <Description>
## <Index>random seed</Index>
## For a dense list <A>list</A>,
## <Ref Func="RandomList"/> returns a (pseudo-)random element with equal
## distribution.
## <P/>
## This function uses the <Ref Var="GlobalMersenneTwister"/> to produce the
## The random source <A>rs</A> is used to choose a random number.
## If <A>rs</A> is absent,
## this function uses the <Ref Var="GlobalMersenneTwister"/> to produce the
## random elements (a source of high quality random numbers).
## <P/>
## </Description>
Expand Down
6 changes: 4 additions & 2 deletions lib/coll.gd
Original file line number Diff line number Diff line change
Expand Up @@ -1631,15 +1631,17 @@ DeclareOperation( "Random", [ IS_INT, IS_INT ] );
##
## <#GAPDoc Label="RandomList">
## <ManSection>
## <Func Name="RandomList" Arg='list'/>
## <Func Name="RandomList" Arg='[rs,] list'/>
##
## <Description>
## <Index>random seed</Index>
## For a dense list <A>list</A>,
## <Ref Func="RandomList"/> returns a (pseudo-)random element with equal
## distribution.
## <P/>
## This function uses the <Ref Var="GlobalMersenneTwister"/> to produce the
## The random source <A>rs</A> is used to choose a random number.
## If <A>rs</A> is absent,
## this function uses the <Ref Var="GlobalMersenneTwister"/> to produce the
## random elements (a source of high quality random numbers).
## <P/>
## </Description>
Expand Down
19 changes: 17 additions & 2 deletions lib/coll.gi
Original file line number Diff line number Diff line change
Expand Up @@ -246,8 +246,23 @@ InstallMethod( RepresentativeSmallest,
## an enumerator of <C> and selects a random element of this list using the
## function `RandomList', which is a pseudo random number generator.
##
InstallGlobalFunction( RandomList, function(list)
return list[Random(GlobalMersenneTwister, 1, Length(list))];

# RandomList is not an operation to avoid the (often expensive) cost of
# dispatch for lists
InstallGlobalFunction( RandomList, function(args...)
local len, source, list;
len := Length(args);
if len = 1 then
source := GlobalMersenneTwister;
list := args[1];
elif len = 2 then
source := args[1];
list := args[2];
else
Error(" Usage: RandomList([rs], list))");
fi;

return list[Random(source, 1, Length(list))];
end );
InstallMethod( Random, "for a (finite) collection",
[ IsCollection and IsFinite ],
Expand Down
6 changes: 6 additions & 0 deletions tst/testinstall/random.tst
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
gap> START_TEST("random.tst");
gap> ReadGapRoot( "tst/testrandom.g" );

# Test RandomList
gap> randomTest([1,2,3], RandomList);
gap> randomTest([1..100], RandomList);
gap> randomTest("abcdef", RandomList);
gap> randomTestForSizeOneCollection([1], RandomList);

#
# fields and rings
#
Expand Down

0 comments on commit 40bc9ac

Please sign in to comment.