File indexing completed on 2024-04-06 12:32:55
0001 #include "Random.h"
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032 static const int SEED = 113;
0033
0034 double MonteCarlo_num_flops(int Num_samples) {
0035
0036
0037 return ((double)Num_samples) * 4.0;
0038 }
0039
0040 double MonteCarlo_integrate(int Num_samples) {
0041 Random R = new_Random_seed(SEED);
0042
0043 int under_curve = 0;
0044 int count;
0045
0046 for (count = 0; count < Num_samples; count++) {
0047 double x = Random_nextDouble(R);
0048 double y = Random_nextDouble(R);
0049
0050 if (x * x + y * y <= 1.0)
0051 under_curve++;
0052 }
0053
0054 Random_delete(R);
0055
0056 return ((double)under_curve / Num_samples) * 4.0;
0057 }