-
Notifications
You must be signed in to change notification settings - Fork 0
/
mcparab.c
161 lines (153 loc) · 8.23 KB
/
mcparab.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/* mcparab.c - Monte Carlo Integral of x^2 Version 1.0.0 */
/* Copyright (C) 2016 aquila62 at github.com */
/* This program is free software; you can redistribute it and/or */
/* modify it under the terms of the GNU General Public License as */
/* published by the Free Software Foundation; either version 2 of */
/* the License, or (at your option) any later version. */
/* This program is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU General Public License for more details. */
/* You should have received a copy of the GNU General Public License */
/* along with this program; if not, write to: */
/* Free Software Foundation, Inc. */
/* 59 Temple Place - Suite 330 */
/* Boston, MA 02111-1307, USA. */
/***********************************************************/
/* mcparab estimates the definite integral of x^2 */
/* through a Monte Carlo simulation. */
/* Two uniform random numbers are generated 0-1 */
/* 1 million times */
/* If y < x^2 then tally a counter */
/* The ratio of (total_counter / 1 million) */
/* is approximately 1/3 to 3 decimal places */
/* or approximately 0.333... */
/* with an expected error of 1/1000 */
/***********************************************************/
#include <stdio.h>
#include <time.h>
#include <sys/times.h>
#include <math.h>
#include <gsl/gsl_rng.h>
#include "etaus.h"
#ifndef M_PI
#define M_PI (3.14159265358979323846)
#endif
int main(void)
{
int i; /* loop counter */
int etauselap; /* elapsed time for etaus */
int mtelap; /* elapsed time for mt19937 */
int ranlxelap; /* elapsed time for ranlxd2 */
unsigned int dttk; /* combined date, time #ticks */
double tot; /* total points */
double bot; /* 1 million points */
double ratio; /* estimated 1/3 */
time_t now; /* current date and time */
clock_t clk; /* current number of ticks */
clock_t etausstart; /* start time for etaus */
clock_t etausfin; /* end time for etaus */
clock_t mtstart; /* start time for mt19937 */
clock_t mtfin; /* end time for mt19937 */
clock_t ranlxstart; /* start time for ranlxd2 */
clock_t ranlxfin; /* end time for ranlxd2 */
struct tms t; /* structure used by times() */
gsl_rng *r; /* GSL RNG structure */
etfmt *et; /* etaus structure */
et = (etfmt *) etausinit(); /* initialize the etaus structure */
bot = 1000000.0; /* set to 1 million */
/************************************************************/
tot = 0.0; /* initialize total points */
i = (int) bot; /* set loop counter */
/* get clock ticks since boot */
etausstart = times(&t); /* start time for etaus */
while (i--) /* loop 1 million times */
{
double x; /* horizontal coordinate */
double y; /* vertical coordinate */
double wyy; /* y = x*x */
x = etausunif(et); /* uniform number 0-1 */
y = etausunif(et); /* uniform number 0-1 */
wyy = x*x; /* the parabolic curve */
if (y < wyy) tot += 1.0; /* if below parabola, tally */
} /* for each point above or below a parabolic curve */
ratio = tot / bot; /* calculate est. 1/3 */
etausfin = times(&t); /* finish time for etaus */
printf("Monte Carlo Definite Integral of x^2\n");
printf(" From zero to one\n");
printf(" n = 1 million\n");
printf(" Expected error is 1/1000\n");
printf(" etaus %18.15f\n", ratio);
/************************************************************/
/* allocate the mt19937 random number generator */
r = (gsl_rng *) gsl_rng_alloc(gsl_rng_mt19937);
/* get clock ticks since boot */
clk = times(&t);
/* get date & time */
time(&now);
/* combine date, time, and ticks into a single UINT */
dttk = (unsigned int) (now ^ clk);
/* initialize the GSL Mersenne Twister */
/* random number generator to date,time,#ticks */
gsl_rng_set(r, dttk); /* initialize mt19937 seed */
tot = 0.0; /* initialize total points */
i = (int) bot; /* set loop counter */
/* get clock ticks since boot */
mtstart = times(&t); /* start time for GSL mt19937 */
while (i--) /* loop 1 million times */
{
double x; /* horizontal coordinate */
double y; /* vertical coordinate */
double wyy; /* y = x*x */
/* use the mt19937 random number generator this time */
x = gsl_rng_uniform(r); /* uniform number 0-1 */
y = gsl_rng_uniform(r); /* uniform number 0-1 */
wyy = x*x; /* the parabolic curve */
if (y < wyy) tot += 1.0; /* if below parabola, tally */
} /* for each point above or below a parabolic curve */
ratio = tot / bot; /* calculate est. 1/3 */
mtfin = times(&t); /* finish time for GSL mt19937 */
printf("GSL mt19937 %18.15f\n", ratio);
gsl_rng_free(r);
/************************************************************/
/* allocate the ranlxd2 random number generator */
r = (gsl_rng *) gsl_rng_alloc(gsl_rng_ranlxd2);
/* get clock ticks since boot */
clk = times(&t);
/* get date & time */
time(&now);
/* combine date, time, and ticks into a single UINT */
dttk = (unsigned int) (now ^ clk);
/* initialize the GSL ranlxd2 random number generator */
/* to date,time,#ticks */
gsl_rng_set(r, dttk); /* initialize ranlxd2 seed */
tot = 0.0; /* initialize total points */
i = (int) bot; /* set loop counter */
/* get clock ticks since boot */
ranlxstart = times(&t); /* start time for GSL ranlxd2 */
while (i--) /* loop 1 million times */
{
double x; /* horizontal coordinate */
double y; /* vertical coordinate */
double wyy; /* y = x*x */
/* use the ranlxd2 random number generator this time */
x = gsl_rng_uniform(r); /* uniform number 0-1 */
y = gsl_rng_uniform(r); /* uniform number 0-1 */
wyy = x*x; /* the parabolic curve */
if (y < wyy) tot += 1.0; /* if below parabola, tally */
} /* for each point above or below a parabolic curve */
ratio = tot / bot; /* calculate est. 1/3 */
ranlxfin = times(&t); /* finish time for GSL ranlxd2 */
printf("GSL ranlxd2 %18.15f\n", ratio);
printf(" Actual %18.15f\n", 1.0 / 3.0);
etauselap = etausfin - etausstart;
mtelap = mtfin - mtstart;
ranlxelap = ranlxfin - ranlxstart;
printf(" etaus ticks %6d\n", etauselap);
printf("GSL mt19937 ticks %6d\n", mtelap);
printf("GSL ranlxd2 ticks %6d\n", ranlxelap);
gsl_rng_free(r);
free(et->state);
free(et);
return(0);
} /* main */