trandom.pp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. { %GRAPH }
  2. { %INTERACTIVE }
  3. {
  4. This program test the random function
  5. It gets 10M random values
  6. that are placed in 10000 windows
  7. and print the number of occurence for each window
  8. and the profile of the distribution
  9. of the counts
  10. - this gave very bad value due to a modulo problem
  11. but after this solved
  12. it still shows strange wings !!
  13. }
  14. program test_random;
  15. uses
  16. {$ifdef go32v2}
  17. dpmiexcp,
  18. {$endif go32v2}
  19. graph;
  20. const max = 1000;
  21. maxint = 10000*max;
  22. var x : array[0..max-1] of longint;
  23. y : array[-100..100] of longint;
  24. mean,level,i : longint;
  25. maxcount,delta,maximum,minimum : longint;
  26. st,st2 : string;
  27. gm,gd : integer;
  28. color : longint;
  29. begin
  30. {$ifdef go32v2}
  31. gm:=m640x400x256;
  32. gd:=vesa;
  33. {$else }
  34. gd:=detect;
  35. {$endif }
  36. InitGraph(gd,gm,'\tp\bgi');
  37. {$ifdef FPC}
  38. SetWriteMode(NormalPut);
  39. {$endif FPC}
  40. SetColor(red);
  41. color:=blue;
  42. mean:=maxint div max;
  43. setfillstyle(solidfill,blue);
  44. for level:=0 to 10 do
  45. begin
  46. for i:=0 to max-1 do
  47. x[i]:=0;
  48. for i:=-100 to 100 do
  49. y[i]:=0;
  50. for i:=0 to maxint-1 do
  51. begin
  52. if level=0 then
  53. inc(x[trunc(random*max)])
  54. else
  55. inc(x[random(max*level) div (level)]);
  56. if i mod (maxint div 10) = 0 then
  57. begin
  58. bar(20+textwidth('iteration '),17,
  59. 20+textwidth('iteration 0000000'),26);
  60. st:='';
  61. str(i,st);
  62. st:='iteration '+st;
  63. OutTextXY(20,20,st);
  64. {Writeln(stderr,st);}
  65. end;
  66. end;
  67. maximum:=0;
  68. minimum:=$7FFFFFFF;
  69. maxcount:=0;
  70. for i:=0 to max-1 do
  71. begin
  72. if x[i]>maximum then
  73. maximum:=x[i];
  74. if x[i]<minimum then
  75. minimum:=x[i];
  76. if abs(x[i]-mean)<100 then
  77. inc(y[x[i]-mean]);
  78. end;
  79. if maximum=0 then
  80. inc(maximum);
  81. for i:=-100 to 100 do
  82. if y[i]>maxcount then
  83. maxcount:=y[i];
  84. if maxcount=0 then
  85. inc(maxcount);
  86. OutTextXY(GetMaxX div 2,GetMaxY-30,'Random Test Program');
  87. str(level,st);
  88. st:='Level '+st;
  89. bar(30,GetMaxY-65,
  90. 30+textwidth(st),getMaxY-52);
  91. OutTextXY(30,GetMaxY-59,st);
  92. str(maximum,st);
  93. str(minimum,st2);
  94. st:='Maximum = '+st+' Minimum ='+st2;
  95. bar(30,GetMaxY-35,
  96. 30+Textwidth(st),getMaxY-22);
  97. OutTextXY(30,GetMaxY-29,st);
  98. for i:=0 to max-1 do
  99. putpixel( (i*getmaxX) div max,
  100. GetMaxY-(x[i]*getMaxY) div (2*mean), color);
  101. inc(color);
  102. setColor(color);
  103. delta:=maximum-minimum+1;
  104. for i:=-100 to 100 do
  105. begin
  106. if i=minimum then
  107. moveto( ((i+100)*getMaxX) div 201,
  108. GetMaxY-(y[i]*getMaxY) div maxcount)
  109. else
  110. lineto( ((i+100)*getMaxX) div 201,
  111. GetMaxY-(y[i]*getMaxY) div maxcount);
  112. if y[i]>0 then
  113. circle( ((i+100)*getMaxX) div 201,
  114. GetMaxY-(y[i]*getMaxY) div maxcount,5);
  115. end;
  116. readln;
  117. inc(color);
  118. end;
  119. CloseGraph;
  120. end.