RosettaCodeData/Task/Monte-Carlo-methods/FutureBasic/monte-carlo-methods.basic

49 lines
1010 B
Plaintext

// Get PI using MonteCarlo method
//
// FutureBasic 7.0.34, August 2025 R.W
// In my opinion, iteration below
// a hundred million won't even show anything
// remotely close to PI
local fn MC_PI(rolls as double) as double
double i, inCircle, dist, MaxINT
double rndX, rndY, result
MaxINT = 2147483647.0
inCircle = 0
for i = 1 TO rolls
// a square with a side of length 2 centered at 0 has
// x and y range of -1 to 1
if i % 2 == 0
rndX = (rnd(MaxINT)-1)/MaxINT
rndY = (rnd(MaxINT)-1)/MaxINT
else
rndX = (rnd(MaxINT)+1)/MaxINT
rndY = (rnd(MaxINT)+1)/MaxINT
end if
dist = rndX ^ 2 + rndY ^ 2
if dist < 1.0 //circle with diameter of 2 has radius of 1
inCircle++
end if
next i
result = 4.0 * inCircle / rolls
end fn = result
window 1,@"Monte Carlo PI"
random
double pi2
pi2 = fn MC_PI(10^4)
print " 10,000 = ";pi2
pi2 = fn MC_PI(10^6)
print " 1,000,000 = ";pi2
pi2 = fn MC_PI(10^8)
print "100,000,000 = ";pi2
HandleEvents