* extended with a Chi square test to check if the random values are equally distributed

This commit is contained in:
florian 2024-05-26 23:16:50 +02:00
parent 39f7172ee8
commit 5a59ffdca7

View File

@ -1,8 +1,12 @@
{ %INTERACTIVE }
program test_random;
const
buckets = 1000000;
repeats = 20;
var
i: integer;
hist : array[1..buckets] of LongInt;
i : longint;
chisquare : extended;
begin
randomize;
WriteLn('Random Values II:');
@ -16,4 +20,20 @@ begin
begin
WriteLn(Random(100));
end;
{ do a Chi^2 test, hopefully I got it right }
fillchar(hist,sizeof(hist),0);
for i:=1 to buckets*repeats do
inc(hist[longint(trunc(random*buckets)+1)]);
chisquare:=0.0;
for i:=low(hist) to high(hist) do
chisquare:=chisquare+sqr(hist[i]-repeats)/repeats;
writeln(chisquare);
{ m=1000000; p=0.1 }
if chisquare>1001741 then
halt(1)
else
writeln('ok');
end.