Browse Source

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

florian 1 year ago
parent
commit
5a59ffdca7
1 changed files with 22 additions and 2 deletions
  1. 22 2
      tests/test/units/system/trandom.pp

+ 22 - 2
tests/test/units/system/trandom.pp

@@ -1,8 +1,12 @@
-{ %INTERACTIVE }
 program test_random;
 program test_random;
 
 
+const
+  buckets = 1000000;
+  repeats = 20;
 var
 var
- i: integer;
+  hist : array[1..buckets] of LongInt;
+  i : longint;
+  chisquare : extended;
 begin
 begin
   randomize;
   randomize;
   WriteLn('Random Values II:');
   WriteLn('Random Values II:');
@@ -16,4 +20,20 @@ begin
    begin
    begin
      WriteLn(Random(100));
      WriteLn(Random(100));
    end;
    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.
 end.