-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunit1.pas
217 lines (193 loc) · 5.43 KB
/
unit1.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls, ActnList,
Menus, StdActns, TAGraph, TASeries, TASources;
type
{ TForm1 }
TForm1 = class(TForm)
FileExitAction: TFileExit;
FileSaveAsAction: TFileSaveAs;
MainMenu1: TMainMenu;
MenuItem10: TMenuItem;
MenuItem11: TMenuItem;
MenuItem2: TMenuItem;
MenuItem3: TMenuItem;
MenuItem4: TMenuItem;
MenuItem5: TMenuItem;
MenuItem6: TMenuItem;
MenuItem7: TMenuItem;
MenuItem8: TMenuItem;
MenuItem9: TMenuItem;
RandomEditItem: TMenuItem;
RandomEditAction: TAction;
ActionList1: TActionList;
Chart1: TChart;
Chart1LineSeries1: TLineSeries;
Panel1: TPanel;
procedure FileSaveAsActionAccept(Sender: TObject);
procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure RandomEditActionExecute(Sender: TObject);
private
public
Calculating: Boolean;
NumberCount: int64;
Sample, SampleCount: Integer;
Source: TListChartSource;
procedure CalcChart;
function IndexOf(x: Double): Integer;
procedure UpdateChart;
end;
var
Form1: TForm1;
implementation
uses FormEx, RndEdFrm, StatForm;
{$R *.lfm}
{ TForm1 }
procedure TForm1.CalcChart;
var
i, n: int64;
x: Extended;
begin
if Calculating then Exit;
Calculating := True;
Chart1LineSeries1.BeginUpdate;
Source.Clear;
Randomize;
if not Assigned(StatusForm) then StatusForm := TStatusForm.Create(Self);
StatusForm.Show;
if NumberCount > 0 then begin
for i := 0 to NumberCount - 1 do Source.Add(i, 0);
for i := 1 to SampleCount do begin
Sample := i;
n := Random(NumberCount);
Source.Item[n]^.Y := Source.Item[n]^.Y + 1;
if i mod 1000 = 0 then begin
{StatusForm.Label1.Caption := Format('%:7.3f %% fertig', [i / SampleCount * 100]);}
Application.ProcessMessages;
if not Calculating then Break;
end;
end;
{for i := NumberCount - 1 downto 0 do
if Source.Item[i]^.Y = 0 then Source.Delete(i);}
Panel1.Caption := Format('Häufigkeit der ganzen Zufallszahlen 0 bis %d in %d Versuchen', [NumberCount - 1, SampleCount]);
Chart1.Title.Text.Text := Panel1.Caption;
end
else begin
for i := 1 to SampleCount do begin
Sample := i;
x := Random;
{ShowMessage('Index finden');}
n := IndexOf(x);
{ShowMessageFmt('Index %d aktueisieren oder erstellen', [n]);}
if n >= 0 then Source.Item[n]^.Y := Source.Item[n]^.Y + 1
else Source.Add(x, 1);
if Sample mod 1000 = 0 then begin
{StatusForm.Label1.Caption := Format('%:7.3f %% fertig', [i / SampleCount * 100]);}
Application.ProcessMessages;
if not Calculating then Break
end;
end;
Panel1.Caption := Format('Häufigkeit der gebrochenen Zufallszahlen in [0, 1) nach %d Versuchen', [SampleCount]);
Chart1.Title.Text.Text := Panel1.Caption;
end;
for i := 0 to Source.Count - 1 do begin
Source.Item[i]^.Y := Source.Item[i]^.Y / SampleCount
end;
StatusForm.Close;
Chart1LineSeries1.EndUpdate;
Calculating := False
end;
function TForm1.IndexOf(x: Double): Integer;
var
i: Integer;
begin
Result := -1;
for i := 0 to Source.Count - 1 do
if x = Source.Item[i]^.X then begin
Result := i;
Exit
end
else if x > Source.Item[i]^.X then Exit;
end;
procedure TForm1.UpdateChart;
var
a, b, c: Double;
i: Integer;
begin
Chart1LineSeries1.BeginUpdate;
if Source.Count > 0 then
with Chart1.LeftAxis.Range do begin
if Source.Count = 1 then begin
a := Source.Item[0]^.Y * 0.9;
b := Source.Item[0]^.Y * 1.1;
end
else begin
a := Source.Item[0]^.Y;
b := Source.Item[1]^.Y;
if a > b then begin
c := a; a := b; b := c
end;
for i := 2 to Source.Count - 1 do
if a > Source.Item[i]^.Y then a := Source.Item[i]^.Y
else if b < Source.Item[i]^.Y then b := Source.Item[i]^.Y
end;
if a = b then begin
Min := a * 0.9;
Max := b * 1.1;
end
else begin
Min := a;
Max := b
end;
UseMin := True;
UseMax := True;
{ShowMessageFmt('[%g, %g]', [Min, Max])}
end;
Chart1LineSeries1.EndUpdate;
Calculating := False;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Source := TListChartSource(Chart1LineSeries1.Source);
Source.Sorted := True;
NumberCount := 0;
SampleCount := 100000;
Show;
CalcChart;
{ShowMessage('Serie berechnet');}
UpdateChart;
FormAdjust(Self);
end;
procedure TForm1.FileSaveAsActionAccept(Sender: TObject);
begin
Chart1.SaveToBitmapFile(FileSaveAsAction.Dialog.FileName);
end;
procedure TForm1.FormClose(Sender: TObject; var CloseAction: TCloseAction);
begin
Calculating := False
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
end;
procedure TForm1.RandomEditActionExecute(Sender: TObject);
begin
with RandomEditForm do begin
SampleCountEdit.Text:= IntToStr(SampleCount);
CaseCountEdit.Text := IntToStr(NumberCount);
ChartUpdateCheckBox.Checked := False;
case ShowModal of
mrOK: begin
Application.ProcessMessages;
SampleCount := StrToInt(SampleCountEdit.Text);
NumberCount := StrToInt(CaseCountEdit.Text);
CalcChart;
if ChartUpdateCheckBox.Checked then UpdateChart;
end;
end;
end;
end;
end.