2
0

tw35878.pp 867 B

123456789101112131415161718192021222324252627282930313233343536
  1. program random_test;
  2. {$mode objfpc}{$H+}
  3. {$APPTYPE CONSOLE}
  4. const
  5. l: UInt64 = 6148914691236517205;
  6. var
  7. s,n: UInt64;
  8. i,j: UInt64;
  9. begin
  10. WriteLn('Experiment:', LineEnding);
  11. WriteLn(' Draw a natural number r from the intervall [0,l-1] and');
  12. WriteLn(' increment a counter s when r < l div 2 is satisfied.');
  13. WriteLn(' Repeat this step n times and calculate the ratio s/n.', LineEnding);
  14. WriteLn(' Expected ratio: ', (l div 2)/l:30, LineEnding);
  15. WriteLn('Input size n':16, 'Observed ratio s/n':30);
  16. l := 6148914691236517205;
  17. j := 4;
  18. while j <= 18 do
  19. begin
  20. n := (UInt64(1) shl j);
  21. s := 0;
  22. i := 0;
  23. while i <= n-1 do
  24. begin
  25. if Random(Int64(l)) < l div 2 then
  26. Inc(s);
  27. Inc(i);
  28. end;
  29. WriteLn( (UInt64(1) shl j):16, s/n:30);
  30. Inc(j);
  31. end;
  32. if abs(0.5-(s/n))>0.1 then
  33. halt(1);
  34. end.