Pi simulator written with Delphi
Pi ? is an old thing, as early as 1700 years ago, has its value Zu Chong Zhi accurate to seven decimal places, and now through the computer program has been able to count to 1 billion much! I was pi lovers, this “hobby” at least out of this irrational enthusiasm, not only can I recite to 100 decimal places, but also a collection of several computing ? from the Internet to find the program you are interested can go to my The small station nihg.yeah.net look for it.
First, simulator principle
This program and use the calculated value of the algorithm ?, the probability that she is only a simulation, that the square side length of 200 randomly generate points, the point to separate the arc statistics for the sector, due to the number of points a lot, until almost covered the entire region. At this point, the number of points that it can be seen as an area in the region. Can be derived as follows:
The number of blue spots in the region: the total number of ??Blue Area: total area
The number of blue spots in the region: the total number of ??circle area / 4: The total area
The number of blue spots in the region: the total number of ??? ? 200 ? 200 / 4:200 ? 200
? ??4 ? the number of blue spots in the region / total number of
Of course, the point position to repeat, so the results with the ? value is a difference, but, when the point long enough, you can see a very similar result.
Second, programming
Open Delphi 7, the first design the form according to Figure 1:
Image1 control program first in the region to draw a square of side length of 200 presentations as a program window, so FormCreate event as follows:
procedure TForm1.FormCreate (Sender: TObject);
begin
/ / Draw bright green square presentation box
Image1.Canvas.Brush.Color: = clBlack;
Image1.Canvas.FillRect (Rect (0,0,199,199));
Image1.Canvas.Pen.Color: = cllime;
Image1.Canvas.Rectangle (0,0,199,199);
DoubleBuffered: = True;
end;
Then add a Timer to achieve point mapping and the calculation of ?:
procedure TForm1.Timer1Timer (Sender: TObject);
var
a, b, i, ii: longint;
pi, piok: single;
begin
/ / Randomly generated coordinates
i: = random (200);
ii: = random (200);
if (i * i + ii * ii <40000) then
begin
/ / Radius of 200 points set a blue circle
Image1.canvas.Pen.Color: = claqua;
a: = StrToInt (Label1.Caption);
Label1.Caption: = IntToStr (a +1);
/ / Display the current coordinates of points
Label6.Caption: = IntToStr (Image1.Canvas.PenPos.X);
Label7.Caption: = IntToStr (Image1.Canvas.PenPos.Y);
end
else
begin
/ / Beyond the point of this region are peripheral to the yellow circle
Image1.Canvas.Pen.Color: = clyellow;
b: = StrToInt (Label2.Caption);
Label2.Caption: = IntToStr (b +1);
end;
/ / Draw point (1 pixel length of a straight line)
Image1.Canvas.MoveTo (i, ii);
Image1.Canvas.LineTo (i, ii +1);
/ / Calculate the value of pi
pi: = (4 * (StrToInt (Label1.Caption)) / (StrToInt (Label1.Caption) + StrToInt (Label2.Caption)));
Label3.Caption: = FloatToStr (pi);
/ / Label4 shows the closest real value of pi.
piok: = StrToFloat (Label4.Caption);
/ / Draw the closest value of pi piok
if (abs (pi-3.141592653589)) < (abs (piok-3.141592653589)) then
Label4.Caption: = FloatToStr (pi);
end;
Finally add two SpeedButton as start and pause button, the code are Timer1.Enabled: = true; and Timer1.Enabled: = false;. Well, this process is complete, then click the F9 personally simulation calculation of ? about it!
III Summary
Although the program does not use pi's algorithm, but through a random number of ? to approximate the point Miaocheng by countless wonderful arc so that we breath. In fact, this idea can be used for many occasions, such as on a mathematical theorem or the laws of nature (lottery?), The simulation, the idea that you hope to inspire.
????:
Sun head of the delisting refuted rumors are UNFOUNDED conspiracy
8-year restructuring Neusoft normalized
Articles about Gallery And Cataloging Tools
E-cology in the Pan Micro Series 67
Project Management Introduction
Dell desktop computers next month and push AMD 1 month earlier than expected

Husband researchers-and volunteers, encore lexically solid poor.
Comment by louis vuitton bags — December 7, 2010 @ 5:48 am