Skip to content

Commit

Permalink
hpcgap: sync lib/random.g
Browse files Browse the repository at this point in the history
  • Loading branch information
fingolfin committed Jan 12, 2018
1 parent 19317fe commit 61bb2e5
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 78 deletions.
70 changes: 0 additions & 70 deletions hpcgap/lib/random.g

This file was deleted.

44 changes: 37 additions & 7 deletions lib/random.g
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,30 @@
##
#F RANDOM_LIST( <list> ) . . . . . . . . return a random element from a list
##
R_N := 1;
R_X := [];
if IsHPCGAP then
MakeThreadLocal("R_N");
MakeThreadLocal("R_X");
else
R_N := 1;
R_X := [];
fi;

# 268435456 is 2^28. This way
# we avoid recomputing it every time we need it.
R_228 := 2^28;
# We need to compute modulo 2^28 repeatedly. If we just write 2^28 into the
# code, though, GAP needs to evaluate it to 268435456 each time it executes
# the function. To avoid this, we put it into a constant. As an additional
# trick, we actually store -2^28, which gives identical results, but has the
# benefit of being an immediate integer even on 32 bit systems.
BIND_CONSTANT("R_228", -2^28);
RANDOM_LIST := function ( list )
R_N := R_N mod 55 + 1;
R_X[R_N] := (R_X[R_N] + R_X[(R_N+30) mod 55+1]) mod R_228;
return list[ QUO_INT( R_X[R_N] * LEN_LIST(list), R_228 ) + 1 ];
return list[ QUO_INT( R_X[R_N] * LEN_LIST(list), -R_228 ) + 1 ];
end;

RANDOM_SEED := function ( n )
local i;
R_N := 1; R_X := [ n mod R_228 ];
R_N := 1;
R_X := [ n mod R_228 ];
for i in [2..55] do
R_X[i] := (1664525 * R_X[i-1] + 1) mod R_228;
od;
Expand All @@ -38,8 +47,29 @@ RANDOM_SEED := function ( n )
od;
end;

if IsHPCGAP then

BIND_GLOBAL("RANDOM_SEED_COUNTER", FixedAtomicList(1, 0));

BIND_GLOBAL("GET_RANDOM_SEED_COUNTER", function()
local r;
r := ATOMIC_ADDITION(RANDOM_SEED_COUNTER, 1, 1);
return r;
end);

BIND_GLOBAL("RANDOM_SEED_CONSTRUCTOR", function()
RANDOM_SEED( GET_RANDOM_SEED_COUNTER() );
end);

BindThreadLocalConstructor("R_N", RANDOM_SEED_CONSTRUCTOR);
BindThreadLocalConstructor("R_X", RANDOM_SEED_CONSTRUCTOR);

else

if R_X = [] then RANDOM_SEED( 1 ); fi;

fi;

#############################################################################
##
#E random.g . . . . . . . . . . . . . . . . . . . . . . . . . . . ends here
Expand Down
2 changes: 1 addition & 1 deletion lib/random.gi
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ InstallMethod(Random, [IsGAPRandomSource, IsList], function(rs, list)
rn := rs!.R_N mod 55 + 1;
rs!.R_N := rn;
rx[rn] := (rx[rn] + rx[(rn+30) mod 55+1]) mod R_228;
return list[ QUO_INT( rx[rn] * LEN_LIST(list), R_228 ) + 1 ];
return list[ QUO_INT( rx[rn] * LEN_LIST(list), -R_228 ) + 1 ];
else
return list[Random(rs, 1, Length(list))];
fi;
Expand Down

0 comments on commit 61bb2e5

Please sign in to comment.