/[projects]/dao/DelphiScanner/Components/tpsystools_4.04/examples/Delphi/ExRndU.pas
ViewVC logotype

Annotation of /dao/DelphiScanner/Components/tpsystools_4.04/examples/Delphi/ExRndU.pas

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2671 - (hide annotations) (download)
Tue Aug 25 18:15:15 2015 UTC (8 years, 10 months ago) by torben
File size: 13080 byte(s)
Added tpsystools component
1 torben 2671 (* ***** BEGIN LICENSE BLOCK *****
2     * Version: MPL 1.1
3     *
4     * The contents of this file are subject to the Mozilla Public License Version
5     * 1.1 (the "License"); you may not use this file except in compliance with
6     * the License. You may obtain a copy of the License at
7     * http://www.mozilla.org/MPL/
8     *
9     * Software distributed under the License is distributed on an "AS IS" basis,
10     * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11     * for the specific language governing rights and limitations under the
12     * License.
13     *
14     * The Original Code is TurboPower SysTools
15     *
16     * The Initial Developer of the Original Code is
17     * TurboPower Software
18     *
19     * Portions created by the Initial Developer are Copyright (C) 1996-2002
20     * the Initial Developer. All Rights Reserved.
21     *
22     * Contributor(s):
23     *
24     * ***** END LICENSE BLOCK ***** *)
25    
26     unit ExRndU;
27    
28     interface
29    
30     uses
31     Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
32     Dialogs, ComCtrls, StdCtrls, ExtCtrls,
33    
34     StRandom;
35    
36     type
37     TGetRandom = function : double of object;
38    
39     type
40     TForm1 = class(TForm)
41     imgGraph: TImage;
42     cboDist: TComboBox;
43     lblPrompt: TLabel;
44     btnGenerate: TButton;
45     prgGenProgress: TProgressBar;
46     lblGraphTitle: TLabel;
47     lblParms: TLabel;
48     lblParm1: TLabel;
49     lblParm2: TLabel;
50     edtParm1: TEdit;
51     edtParm2: TEdit;
52     lblLeft: TLabel;
53     lblRight: TLabel;
54     updRight: TUpDown;
55     updLeft: TUpDown;
56     lblMaxY: TLabel;
57     procedure btnGenerateClick(Sender: TObject);
58     procedure cboDistChange(Sender: TObject);
59     procedure FormCreate(Sender: TObject);
60     procedure updRightClick(Sender: TObject; Button: TUDBtnType);
61     procedure updLeftClick(Sender: TObject; Button: TUDBtnType);
62     procedure FormDestroy(Sender: TObject);
63     private
64     { Private declarations }
65     public
66     { Public declarations }
67     GraphLeft : double;
68     GraphRight : double;
69     Value1 : double;
70     Value2 : double;
71     PRNG : TStRandomBase;
72     GetRandom : TGetRandom;
73    
74     procedure GenerateGraph(aDistInx : integer);
75    
76     procedure PrepForBeta;
77     procedure PrepForCauchy;
78     procedure PrepForChiSquared;
79     procedure PrepForErlang;
80     procedure PrepForExponential;
81     procedure PrepForF;
82     procedure PrepForGamma;
83     procedure PrepForLogNormal;
84     procedure PrepForNormal;
85     procedure PrepForT;
86     procedure PrepForUniform;
87     procedure PrepForWeibull;
88    
89     function GetBeta : double;
90     function GetCauchy : double;
91     function GetChiSquared : double;
92     function GetErlang : double;
93     function GetExponential : double;
94     function GetF : double;
95     function GetGamma : double;
96     function GetLogNormal : double;
97     function GetNormal : double;
98     function GetT : double;
99     function GetUniform : double;
100     function GetWeibull : double;
101    
102     end;
103    
104     var
105     Form1: TForm1;
106    
107     implementation
108    
109     {$R *.dfm}
110    
111     const
112     DistNames : array [0..11] of string = (
113     'Beta', 'Cauchy', 'ChiSquared', 'Erlang', 'Exponential',
114     'F', 'Gamma', 'LogNormal', 'Normal', 'Student''s t',
115     'Uniform', 'Weibull');
116    
117     const
118     RandomCount = 1000000;
119    
120     procedure TForm1.GenerateGraph(aDistInx : integer);
121     var
122     Buckets : array[0..400] of integer;
123     i : integer;
124     R : double;
125     Inx : integer;
126     MaxHt : integer;
127     MaxLineFactor : double;
128     GraphWidth : double;
129     OldPercent : integer;
130     NewPercent : integer;
131     begin
132     {zero out the buckets}
133     FillChar(Buckets, sizeof(Buckets), 0);
134    
135     {calculate random numbers according to distribution, convert to a
136     bucket index, and increment that bucket count}
137     OldPercent := -1;
138     GraphWidth := imgGraph.Width;
139     for i := 1 to RandomCount do begin
140     NewPercent := (i * 100) div RandomCount;
141     if (NewPercent <> OldPercent) then begin
142     prgGenProgress.Position := NewPercent;
143     OldPercent := NewPercent;
144     end;
145     R := GetRandom;
146     if (GraphLeft <= R) and (R <= GraphRight) then begin
147     Inx := trunc((R - GraphLeft) * GraphWidth / (GraphRight - GraphLeft));
148     if (0 <= Inx) and (Inx <= 400) then
149     inc(Buckets[Inx]);
150     end;
151     end;
152    
153     {calculate the largest bucket}
154     MaxHt := 1;
155     for i := 0 to 400 do
156     if (MaxHt < Buckets[i]) then
157     MaxHt := Buckets[i];
158    
159     {draw the graph}
160     imgGraph.Canvas.Lock;
161     try
162     imgGraph.Canvas.FillRect(Rect(0, 0, imgGraph.Width, imgGraph.Height));
163     MaxLineFactor := imgGraph.Height / MaxHt;
164     imgGraph.Canvas.Pen.Color := clRed;
165     for i := 0 to 400 do begin
166     imgGraph.Canvas.PenPos := Point(i, imgGraph.Height);
167     imgGraph.Canvas.LineTo(i, imgGraph.Height - trunc(Buckets[i] * MaxLineFactor));
168     end;
169     finally
170     imgGraph.Canvas.Unlock;
171     end;
172    
173     lblMaxY.Caption := Format('Max: %8.6f', [MaxHt / RandomCount]);
174     end;
175    
176     procedure TForm1.btnGenerateClick(Sender: TObject);
177     begin
178     if (edtParm1.Text = '') then
179     Value1 := 0.0
180     else
181     Value1 := StrToFloat(edtParm1.Text);
182     if (edtParm2.Text = '') then
183     Value2 := 0.0
184     else
185     Value2 := StrToFloat(edtParm2.Text);
186     GenerateGraph(cboDist.ItemIndex);
187     end;
188    
189     procedure TForm1.cboDistChange(Sender: TObject);
190     begin
191     case cboDist.ItemIndex of
192     0 : PrepForBeta;
193     1 : PrepForCauchy;
194     2 : PrepForChiSquared;
195     3 : PrepForErlang;
196     4 : PrepForExponential;
197     5 : PrepForF;
198     6 : PrepForGamma;
199     7 : PrepForLogNormal;
200     8 : PrepForNormal;
201     9 : PrepForT;
202     10: PrepForUniform;
203     11: PrepForWeibull
204     end;
205     updRightClick(Self, btNext);
206     updLeftClick(Self, btNext);
207     edtParm1.Text := FloatToStr(Value1);
208     edtParm2.Text := FloatToStr(Value2);
209     end;
210    
211     procedure TForm1.PrepForBeta;
212     begin
213     lblParm1.Caption := 'Shape 1:';
214     lblParm1.Visible := true;
215     lblParm2.Caption := 'Shape 2:';
216     lblParm2.Visible := true;
217     edtParm1.Visible := true;
218     edtParm1.Enabled := true;
219     edtParm2.Visible := true;
220     edtParm2.Enabled := true;
221     updLeft.Position := 0;
222     updRight.Position := 1;
223     Value1 := 2.0;
224     Value2 := 4.0;
225     GetRandom := GetBeta;
226     end;
227    
228     procedure TForm1.PrepForCauchy;
229     begin
230     lblParm1.Caption := '(none)';
231     lblParm1.Visible := true;
232     lblParm2.Visible := false;
233     edtParm1.Visible := false;
234     edtParm1.Enabled := false;
235     edtParm2.Visible := false;
236     edtParm2.Enabled := false;
237     updLeft.Position := -5;
238     updRight.Position := 5;
239     Value1 := 0.0;
240     Value2 := 0.0;
241     GetRandom := GetCauchy;
242     end;
243    
244     procedure TForm1.PrepForChiSquared;
245     begin
246     lblParm1.Caption := 'Degrees of freedom:';
247     lblParm1.Visible := true;
248     lblParm2.Visible := false;
249     edtParm1.Visible := true;
250     edtParm1.Enabled := true;
251     edtParm2.Visible := false;
252     edtParm2.Enabled := false;
253     updLeft.Position := 0;
254     updRight.Position := 20;
255     Value1 := 5.0;
256     Value2 := 0.0;
257     GetRandom := GetChiSquared;
258     end;
259    
260     procedure TForm1.PrepForErlang;
261     begin
262     lblParm1.Caption := 'Mean:';
263     lblParm1.Visible := true;
264     lblParm2.Caption := 'Order:';
265     lblParm2.Visible := true;
266     edtParm1.Visible := true;
267     edtParm1.Enabled := true;
268     edtParm2.Visible := true;
269     edtParm2.Enabled := true;
270     updLeft.Position := 0;
271     updRight.Position := 5;
272     Value1 := 1.0;
273     Value2 := 4.0;
274     GetRandom := GetErlang;
275     end;
276    
277     procedure TForm1.PrepForExponential;
278     begin
279     lblParm1.Caption := 'Mean:';
280     lblParm1.Visible := true;
281     lblParm2.Visible := false;
282     edtParm1.Visible := true;
283     edtParm1.Enabled := true;
284     edtParm2.Visible := false;
285     edtParm2.Enabled := false;
286     updLeft.Position := 0;
287     updRight.Position := 10;
288     Value1 := 1.0;
289     Value2 := 0.0;
290     GetRandom := GetExponential;
291     end;
292    
293     procedure TForm1.PrepForF;
294     begin
295     lblParm1.Caption := 'Degrees of freedom 1:';
296     lblParm1.Visible := true;
297     lblParm2.Caption := 'Degrees of freedom 2:';
298     lblParm2.Visible := true;
299     edtParm1.Visible := true;
300     edtParm1.Enabled := true;
301     edtParm2.Visible := true;
302     edtParm2.Enabled := true;
303     updLeft.Position := 0;
304     updRight.Position := 20;
305     Value1 := 10.0;
306     Value2 := 5.0;
307     GetRandom := GetF;
308     end;
309    
310     procedure TForm1.PrepForGamma;
311     begin
312     lblParm1.Caption := 'Shape:';
313     lblParm1.Visible := true;
314     lblParm2.Caption := 'Scale:';
315     lblParm2.Visible := true;
316     edtParm1.Visible := true;
317     edtParm1.Enabled := true;
318     edtParm2.Visible := true;
319     edtParm2.Enabled := true;
320     updLeft.Position := 0;
321     updRight.Position := 10;
322     Value1 := 2.0;
323     Value2 := 1.0;
324     GetRandom := GetGamma;
325     end;
326    
327     procedure TForm1.PrepForLogNormal;
328     begin
329     lblParm1.Caption := 'Mean:';
330     lblParm1.Visible := true;
331     lblParm2.Caption := 'Standard deviation:';
332     lblParm2.Visible := true;
333     edtParm1.Visible := true;
334     edtParm1.Enabled := true;
335     edtParm2.Visible := true;
336     edtParm2.Enabled := true;
337     updLeft.Position := 0;
338     updRight.Position := 10;
339     Value1 := 0.0;
340     Value2 := 1.0;
341     GetRandom := GetLogNormal;
342     end;
343    
344     procedure TForm1.PrepForNormal;
345     begin
346     lblParm1.Caption := 'Mean:';
347     lblParm1.Visible := true;
348     lblParm2.Caption := 'Standard deviation:';
349     lblParm2.Visible := true;
350     edtParm1.Visible := true;
351     edtParm1.Enabled := true;
352     edtParm2.Visible := true;
353     edtParm2.Enabled := true;
354     updLeft.Position := -5;
355     updRight.Position := 5;
356     Value1 := 0.0;
357     Value2 := 1.0;
358     GetRandom := GetNormal;
359     end;
360    
361     procedure TForm1.PrepForT;
362     begin
363     lblParm1.Caption := 'Degrees of freedom:';
364     lblParm1.Visible := true;
365     lblParm2.Visible := false;
366     edtParm1.Visible := true;
367     edtParm1.Enabled := true;
368     edtParm2.Visible := false;
369     edtParm2.Enabled := false;
370     updLeft.Position := -10;
371     updRight.Position := 10;
372     Value1 := 10.0;
373     Value2 := 0.0;
374     GetRandom := GetT;
375     end;
376    
377     procedure TForm1.PrepForUniform;
378     begin
379     lblParm1.Caption := '(none)';
380     lblParm1.Visible := true;
381     lblParm2.Visible := false;
382     edtParm1.Visible := false;
383     edtParm1.Enabled := false;
384     edtParm2.Visible := false;
385     edtParm2.Enabled := false;
386     updLeft.Position := 0;
387     updRight.Position := 1;
388     Value1 := 0.0;
389     Value2 := 0.0;
390     GetRandom := GetUniform;
391     end;
392    
393     procedure TForm1.PrepForWeibull;
394     begin
395     lblParm1.Caption := 'Shape:';
396     lblParm1.Visible := true;
397     lblParm2.Caption := 'Scale:';
398     lblParm2.Visible := true;
399     edtParm1.Visible := true;
400     edtParm1.Enabled := true;
401     edtParm2.Visible := true;
402     edtParm2.Enabled := true;
403     updLeft.Position := 0;
404     updRight.Position := 10;
405     Value1 := 2.0;
406     Value2 := 3.0;
407     GetRandom := GetWeibull;
408     end;
409    
410     function TForm1.GetBeta : double;
411     begin
412     Result := PRNG.AsBeta(Value1, Value2)
413     end;
414    
415     function TForm1.GetCauchy : double;
416     begin
417     Result := PRNG.AsCauchy
418     end;
419    
420     function TForm1.GetChiSquared : double;
421     begin
422     if (Value1 > 65535.0) then
423     raise Exception.Create(
424     'TForm1.GetChiSquared: the degrees of freedom value 1 is too large for this example program');
425     Result := PRNG.AsChiSquared(trunc(Value1))
426     end;
427    
428     function TForm1.GetErlang : double;
429     begin
430     Result := PRNG.AsErlang(Value1, trunc(Value2))
431     end;
432    
433     function TForm1.GetExponential : double;
434     begin
435     Result := PRNG.AsExponential(Value1)
436     end;
437    
438     function TForm1.GetF : double;
439     begin
440     if (Value1 > 65535.0) then
441     raise Exception.Create(
442     'TForm1.GetF: the degrees of freedom value 1 is too large for this example program');
443     if (Value2 > 65535.0) then
444     raise Exception.Create(
445     'TForm1.GetF: the degrees of freedom value 2 is too large for this example program');
446     Result := PRNG.AsF(trunc(Value1), trunc(Value2))
447     end;
448    
449     function TForm1.GetGamma : double;
450     begin
451     Result := PRNG.AsGamma(Value1, Value2)
452     end;
453    
454     function TForm1.GetLogNormal : double;
455     begin
456     Result := PRNG.AsLogNormal(Value1, Value2)
457     end;
458    
459     function TForm1.GetNormal : double;
460     begin
461     Result := PRNG.AsNormal(Value1, Value2)
462     end;
463    
464     function TForm1.GetT : double;
465     begin
466     if (Value1 > 65535.0) then
467     raise Exception.Create(
468     'TForm1.GetT: the degrees of freedom value is too large for this example program');
469     Result := PRNG.AsT(trunc(Value1))
470     end;
471    
472     function TForm1.GetUniform : double;
473     begin
474     Result := PRNG.AsFloat
475     end;
476    
477     function TForm1.GetWeibull : double;
478     begin
479     Result := PRNG.AsWeibull(Value1, Value2)
480     end;
481    
482     procedure TForm1.FormCreate(Sender: TObject);
483     var
484     i : integer;
485     UniformInx : integer;
486     begin
487     cboDist.Items.Clear;
488     UniformInx := -1;
489     for i := 0 to high(DistNames) do begin
490     cboDist.Items.Add(DistNames[i]);
491     if (Copy(DistNames[i], 1, 7) = 'Uniform') then
492     UniformInx := i;
493     end;
494     cboDist.ItemIndex := UniformInx;
495     cboDistChange(Self);
496     PRNG := TStRandomSystem.Create(0);
497     end;
498    
499     procedure TForm1.updRightClick(Sender: TObject; Button: TUDBtnType);
500     begin
501     lblRight.Caption := IntToStr(updRight.Position);
502     GraphRight := updRight.Position;
503     end;
504    
505     procedure TForm1.updLeftClick(Sender: TObject; Button: TUDBtnType);
506     begin
507     lblLeft.Caption := IntToStr(updLeft.Position);
508     GraphLeft := updLeft.Position;
509     end;
510    
511     procedure TForm1.FormDestroy(Sender: TObject);
512     begin
513     PRNG.Free;
514     end;
515    
516     end.

  ViewVC Help
Powered by ViewVC 1.1.20