Correct weird bias in Random64. Testing code that found it is present but commented out. This should be fine.

This commit is contained in:
Eric Fredricksen
2022-09-03 23:14:17 -07:00
parent 99314c4e0e
commit 73754959ac
+28 -3
View File
@@ -835,13 +835,38 @@ var
t: Int64; t: Int64;
function Square(x: Int64): Int64; begin Result := x * x; end; function Square(x: Int64): Int64; begin Result := x * x; end;
function Random64(below: Int64): Int64; function Random64: Int64; overload;
begin begin
Int64Rec(Result).Hi := Random($7FFFFFFF); Int64Rec(Result).Hi := Random($3FFFFFFF);
Int64Rec(Result).Lo := Random($FFFFFFFF); Int64Rec(Result).Lo := Random($FFFFFFFF);
Result := Result mod below;
end; end;
function Random64(below: Int64): Int64; overload;
begin begin
Result := Random64 mod below;
end;
{
procedure TestRandom;
var
i, b: Integer;
c: Integer;
v: Int64;
begin
for b := 0 to 63 do begin
c := 0;
for i := 1 to 1000 do begin
v := Random64($0fffffffffffffff);
if ((v shr b) and 1) = 1 then Inc(c);
end;
OutputDebugString(PChar(IntToStr(b) + ' ' + IntToStr(c) + '\n'));
end
end;
}
begin
// TestRandom;
if Odds(1,2) if Odds(1,2)
then i := Random(Stats.Items.Count) then i := Random(Stats.Items.Count)
else begin else begin