/[projects]/dao/DelphiScanner/Components/tpsystools_4.04/examples/CBuilder/ExRndU.cpp
ViewVC logotype

Annotation of /dao/DelphiScanner/Components/tpsystools_4.04/examples/CBuilder/ExRndU.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2671 - (hide annotations) (download)
Tue Aug 25 18:15:15 2015 UTC (8 years, 9 months ago) by torben
File size: 12163 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     #include <vcl.h>
27     #pragma hdrstop
28    
29     #include "ExRndU.h"
30     //---------------------------------------------------------------------------
31     #pragma link "strandom"
32     #pragma package(smart_init)
33     #pragma resource "*.dfm"
34     TForm1 *Form1;
35    
36     const
37     AnsiString DistNames[12] = {
38     "Beta", "Cauchy", "ChiSquared", "Erlang", "Exponential",
39     "F", "Gamma", "LogNormal", "Normal", "Student's t",
40     "Uniform", "Weibull"};
41    
42    
43     //---------------------------------------------------------------------------
44     __fastcall TForm1::TForm1(TComponent* Owner)
45     : TForm(Owner)
46     {
47     }
48     //---------------------------------------------------------------------------
49     void __fastcall TForm1::FormCreate(TObject *Sender)
50     {
51     int i;
52     int UniformInx;
53    
54     cboDist->Items->Clear();
55     UniformInx = -1;
56     for (i = 0; i < 12; i++) {
57     cboDist->Items->Add(DistNames[i]);
58     if (DistNames[i].AnsiCompare("Uniform") == 0)
59     UniformInx = i;
60     }
61     cboDist->ItemIndex = UniformInx;
62     cboDistChange(this);
63     PRNG = new TStRandomSystem(0);
64     }
65     //---------------------------------------------------------------------------
66     void __fastcall TForm1::FormDestroy(TObject *Sender)
67     {
68     delete PRNG;
69     }
70     //---------------------------------------------------------------------------
71     void __fastcall TForm1::cboDistChange(TObject *Sender)
72     {
73     switch (cboDist->ItemIndex) {
74     case 0 : PrepForBeta(); break;
75     case 1 : PrepForCauchy(); break;
76     case 2 : PrepForChiSquared(); break;
77     case 3 : PrepForErlang(); break;
78     case 4 : PrepForExponential(); break;
79     case 5 : PrepForF(); break;
80     case 6 : PrepForGamma(); break;
81     case 7 : PrepForLogNormal(); break;
82     case 8 : PrepForNormal(); break;
83     case 9 : PrepForT(); break;
84     case 10: PrepForUniform(); break;
85     case 11: PrepForWeibull(); break;
86     }
87     updRightClick(this, btNext);
88     updLeftClick(this, btNext);
89     edtParm1->Text = FloatToStr(Value1);
90     edtParm2->Text = FloatToStr(Value2);
91    
92     }
93     //---------------------------------------------------------------------------
94     void __fastcall TForm1::updRightClick(TObject *Sender, TUDBtnType Button)
95     {
96     lblRight->Caption = IntToStr(updRight->Position);
97     GraphRight = updRight->Position;
98     }
99     //---------------------------------------------------------------------------
100     void __fastcall TForm1::updLeftClick(TObject *Sender, TUDBtnType Button)
101     {
102     lblLeft->Caption = IntToStr(updLeft->Position);
103     GraphLeft = updLeft->Position;
104     }
105     //---------------------------------------------------------------------------
106     void __fastcall TForm1::PrepForBeta()
107     {
108     lblParm1->Caption = "Shape 1:";
109     lblParm1->Visible = true;
110     lblParm2->Caption = "Shape 2:";
111     lblParm2->Visible = true;
112     edtParm1->Visible = true;
113     edtParm1->Enabled = true;
114     edtParm2->Visible = true;
115     edtParm2->Enabled = true;
116     updLeft->Position = 0;
117     updRight->Position = 1;
118     Value1 = 2.0;
119     Value2 = 4.0;
120     GetRandom = GetBeta;
121     }
122    
123     void __fastcall TForm1::PrepForCauchy()
124     {
125     lblParm1->Caption = "(none)";
126     lblParm1->Visible = true;
127     lblParm2->Visible = false;
128     edtParm1->Visible = false;
129     edtParm1->Enabled = false;
130     edtParm2->Visible = false;
131     edtParm2->Enabled = false;
132     updLeft->Position = -5;
133     updRight->Position = 5;
134     Value1 = 0.0;
135     Value2 = 0.0;
136     GetRandom = GetCauchy;
137     }
138    
139     void __fastcall TForm1::PrepForChiSquared()
140     {
141     lblParm1->Caption = "Degrees of freedom:";
142     lblParm1->Visible = true;
143     lblParm2->Visible = false;
144     edtParm1->Visible = true;
145     edtParm1->Enabled = true;
146     edtParm2->Visible = false;
147     edtParm2->Enabled = false;
148     updLeft->Position = 0;
149     updRight->Position = 20;
150     Value1 = 5.0;
151     Value2 = 0.0;
152     GetRandom = GetChiSquared;
153     }
154    
155     void __fastcall TForm1::PrepForErlang()
156     {
157     lblParm1->Caption = "Mean:";
158     lblParm1->Visible = true;
159     lblParm2->Caption = "Order:";
160     lblParm2->Visible = true;
161     edtParm1->Visible = true;
162     edtParm1->Enabled = true;
163     edtParm2->Visible = true;
164     edtParm2->Enabled = true;
165     updLeft->Position = 0;
166     updRight->Position = 5;
167     Value1 = 1.0;
168     Value2 = 4.0;
169     GetRandom = GetErlang;
170     }
171    
172     void __fastcall TForm1::PrepForExponential()
173     {
174     lblParm1->Caption = "Mean:";
175     lblParm1->Visible = true;
176     lblParm2->Visible = false;
177     edtParm1->Visible = true;
178     edtParm1->Enabled = true;
179     edtParm2->Visible = false;
180     edtParm2->Enabled = false;
181     updLeft->Position = 0;
182     updRight->Position = 10;
183     Value1 = 1.0;
184     Value2 = 0.0;
185     GetRandom = GetExponential;
186     }
187    
188     void __fastcall TForm1::PrepForF()
189     {
190     lblParm1->Caption = "Degrees of freedom 1:";
191     lblParm1->Visible = true;
192     lblParm2->Caption = "Degrees of freedom 2:";
193     lblParm2->Visible = true;
194     edtParm1->Visible = true;
195     edtParm1->Enabled = true;
196     edtParm2->Visible = true;
197     edtParm2->Enabled = true;
198     updLeft->Position = 0;
199     updRight->Position = 20;
200     Value1 = 10.0;
201     Value2 = 5.0;
202     GetRandom = GetF;
203     }
204    
205     void __fastcall TForm1::PrepForGamma()
206     {
207     lblParm1->Caption = "Shape:";
208     lblParm1->Visible = true;
209     lblParm2->Caption = "Scale:";
210     lblParm2->Visible = true;
211     edtParm1->Visible = true;
212     edtParm1->Enabled = true;
213     edtParm2->Visible = true;
214     edtParm2->Enabled = true;
215     updLeft->Position = 0;
216     updRight->Position = 10;
217     Value1 = 2.0;
218     Value2 = 1.0;
219     GetRandom = GetGamma;
220     }
221    
222     void __fastcall TForm1::PrepForLogNormal()
223     {
224     lblParm1->Caption = "Mean:";
225     lblParm1->Visible = true;
226     lblParm2->Caption = "Standard deviation:";
227     lblParm2->Visible = true;
228     edtParm1->Visible = true;
229     edtParm1->Enabled = true;
230     edtParm2->Visible = true;
231     edtParm2->Enabled = true;
232     updLeft->Position = 0;
233     updRight->Position = 10;
234     Value1 = 0.0;
235     Value2 = 1.0;
236     GetRandom = GetLogNormal;
237     }
238    
239     void __fastcall TForm1::PrepForNormal()
240     {
241     lblParm1->Caption = "Mean:";
242     lblParm1->Visible = true;
243     lblParm2->Caption = "Standard deviation:";
244     lblParm2->Visible = true;
245     edtParm1->Visible = true;
246     edtParm1->Enabled = true;
247     edtParm2->Visible = true;
248     edtParm2->Enabled = true;
249     updLeft->Position = -5;
250     updRight->Position = 5;
251     Value1 = 0.0;
252     Value2 = 1.0;
253     GetRandom = GetNormal;
254     }
255    
256     void __fastcall TForm1::PrepForT()
257     {
258     lblParm1->Caption = "Degrees of freedom:";
259     lblParm1->Visible = true;
260     lblParm2->Visible = false;
261     edtParm1->Visible = true;
262     edtParm1->Enabled = true;
263     edtParm2->Visible = false;
264     edtParm2->Enabled = false;
265     updLeft->Position = -10;
266     updRight->Position = 10;
267     Value1 = 10.0;
268     Value2 = 0.0;
269     GetRandom = GetT;
270     }
271    
272     void __fastcall TForm1::PrepForUniform()
273     {
274     lblParm1->Caption = "(none)";
275     lblParm1->Visible = true;
276     lblParm2->Visible = false;
277     edtParm1->Visible = false;
278     edtParm1->Enabled = false;
279     edtParm2->Visible = false;
280     edtParm2->Enabled = false;
281     updLeft->Position = 0;
282     updRight->Position = 1;
283     Value1 = 0.0;
284     Value2 = 0.0;
285     GetRandom = GetUniform;
286     }
287    
288     void __fastcall TForm1::PrepForWeibull()
289     {
290     lblParm1->Caption = "Shape:";
291     lblParm1->Visible = true;
292     lblParm2->Caption = "Scale:";
293     lblParm2->Visible = true;
294     edtParm1->Visible = true;
295     edtParm1->Enabled = true;
296     edtParm2->Visible = true;
297     edtParm2->Enabled = true;
298     updLeft->Position = 0;
299     updRight->Position = 10;
300     Value1 = 2.0;
301     Value2 = 3.0;
302     GetRandom = GetWeibull;
303     }
304    
305     double __fastcall TForm1::GetBeta()
306     {
307     return PRNG->AsBeta(Value1, Value2);
308     }
309    
310     double __fastcall TForm1::GetCauchy()
311     {
312     return PRNG->AsCauchy();
313     }
314    
315     double __fastcall TForm1::GetChiSquared()
316     {
317     if (Value1 > 65535.0)
318     throw(Exception(
319     "TForm1.GetChiSquared: the degrees of freedom value 1 is too large for this example program"));
320    
321     return PRNG->AsChiSquared(INT(Value1));
322     }
323    
324     double __fastcall TForm1::GetErlang()
325     {
326     return PRNG->AsErlang(Value1, Value2);
327     }
328    
329     double __fastcall TForm1::GetExponential()
330     {
331     return PRNG->AsExponential(Value1);
332     }
333    
334     double __fastcall TForm1::GetF()
335     {
336     if (Value1 > 65535.0)
337     throw(Exception(
338     "TForm1.GetF: the degrees of freedom value 1 is too large for this example program"));
339     if (Value2 > 65535.0)
340     throw(Exception(
341     "TForm1.GetF: the degrees of freedom value 2 is too large for this example program"));
342     return PRNG->AsF(INT(Value1), INT(Value2));
343     }
344    
345     double __fastcall TForm1::GetGamma()
346     {
347     return PRNG->AsGamma(Value1, Value2);
348     }
349    
350     double __fastcall TForm1::GetLogNormal()
351     {
352     return PRNG->AsLogNormal(Value1, Value2);
353     }
354    
355     double __fastcall TForm1::GetNormal()
356     {
357     return PRNG->AsNormal(Value1, Value2);
358     }
359    
360     double __fastcall TForm1::GetT()
361     {
362     if (Value1 > 65535.0)
363     throw(Exception(
364     "TForm1.GetT: the degrees of freedom value is too large for this example program"));
365     return PRNG->AsT(INT(Value1));
366     }
367    
368     double __fastcall TForm1::GetUniform()
369     {
370     return PRNG->AsFloat();
371     }
372    
373     double __fastcall TForm1::GetWeibull()
374     {
375     return PRNG->AsWeibull(Value1, Value2);
376     }
377     //---------------------------------------------------------------------------
378     void __fastcall TForm1::btnGenerateClick(TObject *Sender)
379     {
380     if (edtParm1->Text.IsEmpty())
381     Value1 = 0.0;
382     else
383     Value1 = StrToFloat(edtParm1->Text);
384     if (edtParm2->Text.IsEmpty())
385     Value2 = 0.0;
386     else
387     Value2 = StrToFloat(edtParm2->Text);
388     GenerateGraph(cboDist->ItemIndex);
389     }
390     //---------------------------------------------------------------------------
391     void __fastcall TForm1::GenerateGraph(int aDistInx)
392     {
393     #define RandomCount 1000000
394    
395     int Buckets[401];
396     int i;
397     double R;
398     int Inx;
399     int MaxHt;
400     double MaxLineFactor;
401     double GraphWidth;
402     int OldPercent;
403     int NewPercent;
404     double MaxY;
405    
406     // zero out the buckets
407     ZeroMemory(Buckets, sizeof(Buckets));
408    
409     // calculate random numbers according to distribution, convert to a
410     // bucket index, and increment that bucket count
411     OldPercent = -1;
412     GraphWidth = imgGraph->Width;
413     for (i = 0; i < RandomCount; i++) {
414     NewPercent = (i * 100) / RandomCount;
415     if (NewPercent != OldPercent) {
416     prgGenProgress->Position = NewPercent;
417     OldPercent = NewPercent;
418     }
419     R = GetRandom();
420     if (((double) GraphLeft <= R) && (R <= (double) GraphRight)) {
421     Inx = INT((R - GraphLeft) * GraphWidth / (GraphRight - GraphLeft));
422     if ((0 <= Inx) && (Inx <= 400))
423     Buckets[Inx]++;
424     }
425     }
426    
427     // calculate the largest bucket
428     MaxHt = 1;
429     for (i = 0; i <= 400; i++) {
430     if (MaxHt < Buckets[i])
431     MaxHt = Buckets[i];
432     }
433    
434     // draw the graph
435     imgGraph->Canvas->Lock();
436     try {
437     imgGraph->Canvas->FillRect(Rect(0, 0, imgGraph->Width, imgGraph->Height));
438     MaxLineFactor = (double) imgGraph->Height / MaxHt;
439     imgGraph->Canvas->Pen->Color = clRed;
440     for (i = 0; i <= 400; i++) {
441     imgGraph->Canvas->PenPos = Point(i, imgGraph->Height);
442     imgGraph->Canvas->LineTo(i, imgGraph->Height - INT(Buckets[i] * MaxLineFactor));
443     }
444     }
445     __finally {
446     imgGraph->Canvas->Unlock();
447     }
448    
449     MaxY = (double) MaxHt / RandomCount;
450     lblMaxY->Caption = Format("Max: %8.6f", ARRAYOFCONST((MaxY)));
451     }
452     //---------------------------------------------------------------------------
453    

  ViewVC Help
Powered by ViewVC 1.1.20