unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Mask, Math;

type
  TForm1 = class(TForm)
    Button1: TButton;
    fromEdit: TMaskEdit;
    Label1: TLabel;
    Label2: TLabel;
    NSedit: TMaskEdit;
    toEdit: TMaskEdit;
    Label3: TLabel;
    Label4: TLabel;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure ExecProcess(ACommand: string); forward;

procedure TForm1.Button1Click(Sender: TObject);
var  d,i,v,dfrom,dto,nse  : Integer;
     V1,V2,N,M1,M2,cosV   : Real;
     F                    : TextFile;
begin
     dfrom:=StrToInt(trim(fromEdit.text));
     dto:=StrToInt(trim(toEdit.text));
     nse:=StrToInt(trim(NSedit.text));
     AssignFile(F,'cos.txt');
     Rewrite(F);
     Writeln(F,'dim cos(v1,v2) deg(v1,v2)');
     Writeln(F);
     Randomize;
     for d:=dfrom to dto do
     begin
        Button1.Caption:=IntTostr(d);
        cosV:=0;
        for i:=0 to nse-1 do
        begin
           N:=0;  M1:=0;  M2:=0;
           for v:=0 to d-1 do
           begin
              V1:=1-2*Random;
              V2:=1-2*Random;
              N:=N+(V1*V2);
              M1:=M1+sqr(V1);
              M2:=M2+sqr(V2);
           end;
           M1:=sqrt(M1);
           M2:=sqrt(M2);
           cosV:=cosV+abs(abs(N/(M1*M2)));
        end;
        Writeln(F,d,'   ',FloatToStrF(cosV/nse,ffFixed,7,5),'    ',FloatToStrF(57.2958*arccos(cosV/nse),ffFixed,7,4));
     end;
     CloseFile(F);
     Button1.Caption:='Start';
     ExecProcess('Notepad.exe cos.txt')
end;

procedure ExecProcess(ACommand: string);
var  SI   : TStartupInfo;
     PI   : TProcessInformation;
begin
     FillChar(SI,SizeOf(SI),0);
     with SI do
     begin
        dwFlags:=STARTF_USESHOWWINDOW;
        wShowWindow:=SW_SHOW;
        cb:=SizeOf(TStartupInfo);
     end;
     if CreateProcess(nil,PChar(ACommand),nil,nil,false,NORMAL_PRIORITY_CLASS,nil,nil,SI,PI) then
     with PI do
     begin
        WaitForInputIdle(hProcess,600);
        WaitForSingleObject(hProcess,900);
        WaitForSingleObject(hThread,900);
        CloseHandle(hProcess);
        CloseHandle(hThread);
     end;
end;

end.


