| 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 | #include <iostream> #include <vector> #include <cstdlib> #include <ctime> #include <algorithm> const int POPULATION_SIZE = 100; const int GENERATIONS = 50; const int INITIAL_A = 50;  // Initial number of A alleles const int INITIAL_B = 50;  // Initial number of B alleles class Population { public:     Population() {         alleles.resize(POPULATION_SIZE, 'A');         std::fill(alleles.begin(), alleles.begin() + INITIAL_A, 'A');         std::fill(alleles.begin() + INITIAL_A, alleles.end(), 'B');         std::random_shuffle(alleles.begin(), alleles.end());     }     void simulateGeneration() {         std::vector<char> newAlleles(POPULATION_SIZE);         for (int i = 0; i < POPULATION_SIZE; ++i) {             newAlleles[i] = alleles[std::rand() % POPULATION_SIZE];         }         alleles = newAlleles;     }     void printStatistics() const {         int countA = std::count(alleles.begin(), alleles.end(), 'A');         int countB = std::count(alleles.begin(), alleles.end(), 'B');         std::cout << "A alleles: " << countA << ", B alleles: " << countB << '\n';     } private:     std::vector<char> alleles; }; int main() {     std::srand(static_cast<unsigned>(std::time(0)));     Population population;     std::cout << "Initial population:\n";     population.printStatistics();     for (int generation = 1; generation <= GENERATIONS; ++generation) {         population.simulateGeneration();         std::cout << "Generation " << generation << ":\n";         population.printStatistics();     }     return 0; } | 
Explanation
- Class Definition (Population):- Private Members:
- alleles: A vector storing the alleles in the population (‘A’ or ‘B’).
 
- Public Methods:
- Population(): Constructor initializes the population with- INITIAL_A‘A’ alleles and- INITIAL_B‘B’ alleles. Shuffles the alleles to randomize their order.
- void simulateGeneration(): Simulates genetic drift by randomly selecting alleles to form the new generation. Each individual in the new generation is randomly chosen from the current population.
- void printStatistics() const: Prints the count of ‘A’ and ‘B’ alleles in the current population.
 
 
- Private Members:
- mainFunction:- Initializes the random seed with the current time.
- Creates a Populationobject to start the simulation.
- Prints the initial population statistics.
- Simulates multiple generations, printing the allele statistics for each generation.