-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathradionuc.hpp
57 lines (47 loc) · 1.84 KB
/
radionuc.hpp
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
#pragma once
#include <iostream>
#include <cmath>
class radioNuclide {
// TODO: Implement radioNuclide class
private:
double lambda; // Decay constant
int population; // Current population of nuclei
public:
radioNuclide() : lambda(0.0), population(0) {} // Default constructor
radioNuclide(const double lambda, const int N) : lambda(lambda), population(N) {} // Constructor with parameters lambda and initial population size
// Get the population size
int Count() {
return population;
}
// Simulates radioactive decay for a time interval
int Decay(const double tinterval) {
// Compute probability for a given single nucleus to decay in t interval seconds
double probability = lambda * tinterval;
// Sets the number of atoms decayed as 0 first
int numDecayed = 0;
// Loop through entire population to see which atoms deacy in t interval seconds
for (int i = 0; i < population; ++i) {
double randNum = (double)rand() / RAND_MAX;
// Checks if the random number is less than the probability
if (randNum < probability) {
++numDecayed;
}
}
// Reduce population size by the number of atoms that decayed
population -= numDecayed;
// Returns the number of atoms that decayed
return numDecayed;
}
// Add new radionuclides to the population
void Add(const int N) {
population += N;
}
// Sets population to 0
void Zero() {
population = 0;
}
// Overload of the += operator to add nuclei to the population
void operator+=(const int N) {
Add(N);
}
};