Const n=10000;
var x,y,s,h:real; i,k:integer;
begin
k:=0;
for i:=1 to n do
begin
x:=pi/2*random;
y:=random;
if y end;
s:=pi/2*k/n;
writeln('s = ',s:7:5);
// Проверка (можно убрать)
h:=pi/2/n;
x:=0-h;
s:=0;
for i:=1 to n do
begin
x:=x+h; s:=s+sin(x);
end;
s:=s*h;
writeln('s = ',s:7:5);
end.
Пример:
s = 0.99479
s = 0.99992
QBasic:
Dim x As double
Dim y As double
Dim s As double
Dim h As double
Dim p2 As double
Dim n As integer
Dim i As integer
Dim k As integer
p2=3.1415926535/2
n=10000
k=0
for i=1 to n
x=p2*rnd
y=rnd
if ynext i
s=p2*k/n
print "s = ";s
REM Проверка (можно убрать)
h=p2/n
x=0-h
s=0
for i=1 to n
x=x+h
s=s+sin(x)
next i
s=s*h
print "s = ";s