diff --git a/resources/sourcecode/montecarlo-pi-fix.c b/resources/sourcecode/montecarlo-pi-fix.c new file mode 100644 index 0000000..b960057 --- /dev/null +++ b/resources/sourcecode/montecarlo-pi-fix.c @@ -0,0 +1,22 @@ +#include +#include + +int main(int argc, char* argv[]) { + double x,y,z,count; + + int seed = atoi(argv[1]); // Seed for srand + int n = atoi(argv[2]); // Number of iterations + + srand(seed); + + for (int i = 0; i < n; i++) { + x = (double) rand() / RAND_MAX; + y = (double) rand() / RAND_MAX; + z = x * x + y * y; + if (z <= 1) count++; + } + + printf("π approx.: %g", (count / n) * 4); + + return(0); +} diff --git a/resources/sourcecode/montecarlo-pi.c b/resources/sourcecode/montecarlo-pi.c index 370cf96..41c942b 100644 --- a/resources/sourcecode/montecarlo-pi.c +++ b/resources/sourcecode/montecarlo-pi.c @@ -3,10 +3,11 @@ #include int main(int argc, char* argv[]) { - srand(time(NULL)); - double x,y,z,count; - int n = atoi(argv[1]); + + int n = atoi(argv[1]); // Number of iterations + + srand(time(NULL)); for (int i = 0; i < n; i++) { x = (double) rand() / RAND_MAX; diff --git a/src/thesis/2-reproducibility.typ b/src/thesis/2-reproducibility.typ index 60b8942..e6378a6 100644 --- a/src/thesis/2-reproducibility.typ +++ b/src/thesis/2-reproducibility.typ @@ -373,9 +373,38 @@ probabilistic approach to problem-solving. kind: "terminal", ) -In practice, for certain applications, runtime reproducibility can be attained -by controlling the random number generator, specifically by setting a fixed seed -as an input parameter. +To address the challenge of reproducibility in practice, identifying and +isolating the sources of randomness is crucial. These sources can vary widely +depending on the implementation and may include system time, hardware random +number generators, or other external factors. By isolating these components and +enabling control by the end user (e.g., by passing these sources of randomness +as parameters), it becomes possible to deterministically control the random +sequence. This ensures that, given the same seed, the algorithm produces +identical outputs across executions, preserving its stochastic nature while +enabling reproducibility. + +In the specific case of the Monte Carlo algorithm from @montecarlo-pi.c, this +challenge can often be resolved by passing a seed as an input parameter to the +random number generator, as seen in @montecarlo-pi-fixed.c. The seed is used to +initialise the random number generator, ensuring that the same sequence of +random numbers is generated across different runs. This subtle and simple +adjustment provides a deterministic framework while maintaining the +probabilistic essence of the algorithm. + +Adopting such practices aligns with the established principles of good software +development. These practices promote determinism and predictability, which are +key attributes for creating reliable and maintainable systems in the long run. + +#figure( + { + sourcefile( + file: "montecarlo-pi.c", + lang: "c", + read("../../resources/sourcecode/montecarlo-pi-fix.c"), + ) + }, + caption: [`montecarlo-pi-fix.c` with deterministic random number generator], +) In the next example, the source code is not reproducible at build time and we might erroneously think that the program is reproducible at run time.