Data update

This commit is contained in:
Ingy döt Net 2025-08-11 18:05:26 -07:00
parent 4d5544505c
commit 4924dd0264
3073 changed files with 55820 additions and 4408 deletions

View file

@ -0,0 +1 @@
mc_pi 4 × ÷ (+/1×?+×?)(/0)

View file

@ -1,21 +1,29 @@
#include<iostream>
#include<math.h>
#include<stdlib.h>
#include<time.h>
#include <print>
#include <random>
using namespace std;
int main(){
int jmax=1000; // maximum value of HIT number. (Length of output file)
int imax=1000; // maximum value of random numbers for producing HITs.
double x,y; // Coordinates
int hit; // storage variable of number of HITs
srand(time(0));
for (int j=0;j<jmax;j++){
hit=0;
x=0; y=0;
for(int i=0;i<imax;i++){
x=double(rand())/double(RAND_MAX);
y=double(rand())/double(RAND_MAX);
if(y<=sqrt(1-pow(x,2))) hit+=1; } //Choosing HITs according to analytic formula of circle
cout<<""<<4*double(hit)/double(imax)<<endl; } // Print out Pi number
#include <cmath>
#include <cstddef>
[[nodiscard]] double monte_carlo_pi(std::size_t samples) noexcept {
std::mt19937_64 eng{ std::random_device{}() }; // consider using better seeding
std::uniform_real_distribution<double> dst{};
std::size_t hits = 0;
for (std::size_t i = 0; i < samples; ++i) {
if (std::hypot(dst(eng), dst(eng)) <= 1.0) ++hits;
}
return (static_cast<double>(hits) / samples) * 4;
}
int main() {
// using C++23's `std::println` in order to make printing `double`s accurate;
// this would also work with the classic `std::cout`
std::println("{}", monte_carlo_pi(10));
std::println("{}", monte_carlo_pi(100));
std::println("{}", monte_carlo_pi(1000));
std::println("{}", monte_carlo_pi(10'000));
std::println("{}", monte_carlo_pi(100'000));
std::println("{}", monte_carlo_pi(1'000'000));
std::println("{}", monte_carlo_pi(10'000'000));
std::println("{}", monte_carlo_pi(100'000'000));
std::println("{}", monte_carlo_pi(1'000'000'000));
}

View file

@ -0,0 +1,20 @@
create or replace function mcPi(n) as (
with recursive cte as (
select -1 as i, 0 as count, NULL::DOUBLE as x, NULL::DOUBLE as y
union all
select i+1 as i,
count + if( x*x + y*y <= 1, 1, 0) as count,
random() as x,
random() as y
from cte
where i < n
)
select last( 4 * count / n order by i)
from cte
);
# Examples:
select n, mcPi, format('{:.5f}', (mcPi - pi()) / pi()) as "relative error"
from (select n, mcPi(n) as mcPi
from (select unnest([100, 1000, 10000, 100000, 200000]) as n));

View file

@ -0,0 +1,48 @@
// 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

View file

@ -0,0 +1,21 @@
program monte_carlo;
setrandom(0);
loop for x in [5..8] do
throws := 10**x;
print(throws, " => ", calc_pi(throws));
end loop;
proc calc_pi(throws);
inside := 0;
loop init i := 0; while i<throws step i +:= 1; do
x := random 1.0;
y := random 1.0;
if x*x + y*y <= 1 then
inside +:= 1;
end if;
throws -:= 1;
end loop;
return 4*inside/throws;
end proc;
end program;