10
Monte-Carlo
- #include <iostream>
- #include <stdlib.h>
- #include <time.h>
- int main() {
- const long NTHROWS = 1000000; // how many times do we throw the dart
- unsigned long inside = 0;
- unsigned seed = time( 0 );
- double x, y;
- srand( seed );
- for ( unsigned long i = 0; i < NTHROWS; i++ ) {
- x = float(rand())/RAND_MAX;
- y = float(rand())/RAND_MAX;
- if ( x*x + y*y < 1 )
- inside++;
- }
- std::cout << "PI=" << 4.0*inside/NTHROWS << std::endl;
- }
Comments