Skip to content

Commit

Permalink
chapter 2: update section with Monte Carlo algorithm and a fix
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Dec 3, 2024
1 parent 53c7890 commit 63fd11d
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 6 deletions.
22 changes: 22 additions & 0 deletions resources/sourcecode/montecarlo-pi-fix.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include <stdio.h>
#include <stdlib.h>

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);
}
7 changes: 4 additions & 3 deletions resources/sourcecode/montecarlo-pi.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
#include <time.h>

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;
Expand Down
35 changes: 32 additions & 3 deletions src/thesis/2-reproducibility.typ
Original file line number Diff line number Diff line change
Expand Up @@ -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],
) <montecarlo-pi-fixed.c>

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.
Expand Down

0 comments on commit 63fd11d

Please sign in to comment.