Monte Carlo methods: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 118:
10**7 10000000 samples give Pi=3.141677
</pre>
 
 
 
=={{header|Ada}}==
Line 187 ⟶ 185:
</pre>
 
=={{Headerheader|AutoHotkey}}==
{{AutoHotkey case}}
Source: [http://www.autohotkey.com/forum/topic44657.html AutoHotkey forum] by Laszlo
Line 313 ⟶ 311:
return 0;
}</lang>
 
=={{header|C++}}==
<lang cpp>
#include<iostream>
#include<math.h>
#include<stdlib.h>
#include<time.h>
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
}
</lang>
 
=={{header|C sharp|C#}}==
Line 373 ⟶ 346:
100,000,000:3.1413976
</pre>
 
=={{header|C++}}==
<lang cpp>
#include<iostream>
#include<math.h>
#include<stdlib.h>
#include<time.h>
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
}
</lang>
 
=={{header|Clojure}}==
Line 904 ⟶ 902:
end function
</lang>
 
=={{header|FreeBASIC}}==
<lang freebasic>' version 23-10-2016
Line 1,613 ⟶ 1,612:
 
3.141512000000000</lang>
 
=={{header|Maxima}}==
<lang Maxima>load("distrib");
Line 1,790:
1000000: 3.141596
</pre>
 
=={{header|Perl 6}}==
{{works with|rakudo|2015-09-24}}
We'll consider the upper-right quarter of the unitary disk centered at the origin. Its area is <math>\pi \over 4</math>.
<lang Perl 6>my @random_distances = ([+] rand**2 xx 2) xx *;
 
sub approximate_pi(Int $n) {
4 * @random_distances[^$n].grep(* < 1) / $n
}
 
say "Monte-Carlo π approximation:";
say "$_ iterations: ", approximate_pi $_
for 100, 1_000, 10_000;
</lang>
{{out}}
<pre>Monte-Carlo π approximation:
100 iterations: 2.88
1000 iterations: 3.096
10000 iterations: 3.1168</pre>
 
We don't really need to write a function, though. A lazy list would do:
 
<lang perl6>my @pi = ([\+] 4 * (1 > [+] rand**2 xx 2) xx *) Z/ 1 .. *;
say @pi[10, 1000, 10_000];</lang>
 
=={{header|Phix}}==
Line 1,933 ⟶ 1,909:
MonteCarloPi(1000000) = 3.14349599999999980000
MonteCarloPi(10000000) = 3.14127720000000020000
Press any key</pre>
 
=={{header|Python}}==
Line 2,095 ⟶ 2,071:
 
[Similar output]
 
=={{header|Raku}}==
(formerly Perl 6)
{{works with|rakudo|2015-09-24}}
We'll consider the upper-right quarter of the unitary disk centered at the origin. Its area is <math>\pi \over 4</math>.
<lang perl6>my @random_distances = ([+] rand**2 xx 2) xx *;
 
sub approximate_pi(Int $n) {
4 * @random_distances[^$n].grep(* < 1) / $n
}
 
say "Monte-Carlo π approximation:";
say "$_ iterations: ", approximate_pi $_
for 100, 1_000, 10_000;
</lang>
{{out}}
<pre>Monte-Carlo π approximation:
100 iterations: 2.88
1000 iterations: 3.096
10000 iterations: 3.1168</pre>
 
We don't really need to write a function, though. A lazy list would do:
 
<lang perl6>my @pi = ([\+] 4 * (1 > [+] rand**2 xx 2) xx *) Z/ 1 .. *;
say @pi[10, 1000, 10_000];</lang>
 
=={{header|REXX}}==
10,327

edits