Skip to content

Commit

Permalink
Convert Integers to access a random source
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisJefferson committed Jan 8, 2017
1 parent 67a4114 commit 0f92231
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 6 deletions.
12 changes: 6 additions & 6 deletions lib/integer.gi
Original file line number Diff line number Diff line change
Expand Up @@ -1741,12 +1741,12 @@ NrBitsInt := function ( n )
return nr;
end;

InstallMethod( Random,
"for `Integers'",
true,
[ IsIntegers ], 0,
function( Integers )
return NrBitsInt( Random( [0..2^20-1] ) ) - 10;
InstallMethodWithGlobalRandomGen(Random,
"for a random source and `Integers'", true,
[IsRandomSource, IsIntegers],
0,
function( rg, Integers )
return NrBitsInt( Random( rg, [0..2^20-1] ) ) - 10;
end );


Expand Down
6 changes: 6 additions & 0 deletions tst/testinstall/random.tst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
gap> START_TEST("random.tst");
gap> Read( Filename( DirectoriesLibrary( "tst" ), "testrandom.g" ) );
gap> randomTest(Integers, Random);
gap> randomTest([1..10], Random);
gap> randomTest([1,-6,"cheese", Group(())], Random);
gap> STOP_TEST("random.tst", 1);
83 changes: 83 additions & 0 deletions tst/testrandom.g
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
randomTest := function(collection, method)
local test1, test2, test3, test4, test5, test6, localgen;
# Firstly, we will generate a base list
Init(GlobalMersenneTwister, 6);
test1 := List([1..1000], x -> method(collection));
# test2 should = test1
Init(GlobalMersenneTwister, 6);
test2 := List([1..1000], x -> method(collection));
# test3 should also = test1
Init(GlobalMersenneTwister, 6);
test3 := List([1..1000], x -> method(GlobalMersenneTwister, collection));
# test4 should be different (as it came from a different seed)
Init(GlobalMersenneTwister, 8);
test4 := List([1..1000], x -> method(collection));
# test5 should be the same as test4, as it is made from seed 8
# test6 should be the same as test1. Also, it checks that making test5
# did not touch the global source at all.
Init(GlobalMersenneTwister, 8);
localgen := RandomSource(IsMersenneTwister, 6);
test5 := List([1..1000], x -> method(localgen, collection));
test6 := List([1..1000], x -> method(collection));
if ForAny(Concatenation(test1, test2, test3, test4, test5, test6), x -> not (x in collection) ) then
Print("Random member outside collection: ", collection,"\n");
fi;
if test1 <> test2 then
Print("Random not repeatable: ", collection, "\n");
fi;
if test2 <> test3 then
Print("Random 2-arg vs 1-arg broken: ", collection, "\n");
fi;
if test1 = test4 then
Print("Random not changing with random seed: ", collection, "\n");
fi;
if test1 <> test5 then
Print("Alt gen broken: ", collection, "\n");
fi;
if test4 <> test6 then
Print("Random with a passed in seed effects the global generator: ", collection, "\n");
fi;
end;;

# A special test for collections of size 1
randomTestForSizeOneCollection := function(collection, method)
local i, val, localgen, intlist1, intlist2;
if Size(collection) <> 1 then
Print("randomTestForSizeOneCollection is only for collections of size 1");
return;
fi;

val := Representative(collection);

Init(GlobalMersenneTwister, 6);
intlist1 := List([1..1000], x -> Random([1..10]));

for i in [1..1000] do
if method(collection) <> val then
Print("Random returned something outside collection :", collection, ":", val);
fi;
od;

for i in [1..1000] do
if method(GlobalMersenneTwister, collection) <> val then
Print("Random returned something outside collection :", collection, ":", val);
fi;
od;

localgen := RandomSource(IsMersenneTwister, 6);

Init(GlobalMersenneTwister, 6);
for i in [1..1000] do
if method(localgen, collection) <> val then
Print("Random returned something outside collection :", collection, ":", val);
fi;
od;

# The previous loop should not have effected GlobalMersenneTwister,
# so this should be the same as intlist1
intlist2 := List([1..1000], x -> Random([1..10]));

if intlist1 <> intlist2 then
Print("Random read from local gen effected global gen: ", collection);
fi;
end;;

0 comments on commit 0f92231

Please sign in to comment.