I am making a program that "simulates" genetic breakdown in DNA and simulates population growth of a fake group of beings, what I need to do is simulate beings pairing up with male and female and mating. Each being has a name which is a number and I have a random number generator that outputs a number and that number is a name of a being both male and female and it pairs them up with each other, however I need it to pair up unique males and females, but it just pairs up the same males and females sometimes, so it's doing this:
1 0
1 3
2 0
3 2
as you can see "1" is pairing up multiple times with 0 and 3, and that shouldn't happen. Here is my code, please forgive it, it's quite a mess, I want to get the program working before I split it up into classes and functions:
Code:#include <iostream>#include <string> #include <fstream> #include <time.h> #include <stdlib.h> #include <map> #include <thread> #include <vector> time_t CountTime(int countTime) { double timepassed = 0.0; time_t start = time(NULL); //gets the number of seconds since Jan. 1 2000. time_t end = time(NULL); while(timepassed < countTime) { std::cout << timepassed << std::endl; end = time(NULL); timepassed = difftime(end, start); std::cout << timepassed << std::endl; } return timepassed; } int main() { int maleCurrentPopulation = 4; int femaleCurrentPopulation = 4; int beingCurrentPopulation = maleCurrentPopulation + femaleCurrentPopulation; int beingsThatAreDead = 0; int matingTime = 5; int gestationTime = 30; bool isMale = true; bool isFemale = true; bool isRelated = false; std::vector<std::pair<int, int> > pairBeingsUp; //pairs up one male and female. //Male std::vector<int> M_beingNames; std::vector<std::string> M_beingGenders; std::vector<int> M_cleanDna; std::vector<int> M_defectiveDna; std::vector<int> M_generation; std::vector<std::string> M_relationship; // son, daughter etc. std::vector<std::pair<int, int> > M_parentNames; std::vector<int> M_beingLifespan; std::vector<int> M_beingCurrentAge; std::vector<bool> M_hasMated; std::vector<bool> M_isOffspring; std::vector<bool> M_isCoupled; //Has a mate //Female std::vector<int> F_beingNames; std::vector<std::string> F_beingGenders; std::vector<int> F_cleanDna; std::vector<int> F_defectiveDna; std::vector<int> F_generation; std::vector<std::string> F_relationship; // son, daughter etc. std::vector<std::pair<int, int> > F_parentNames; std::vector<int> F_beingLifespan; std::vector<int> F_beingCurrentAge; std::vector<bool> F_hasMated; std::vector<bool> F_isOffspring; std::vector<bool> F_isCoupled; //Setup Gen 1 Beings //Males for(int i = 0; i < maleCurrentPopulation; i++) { M_beingNames.push_back(i); M_beingGenders.push_back("Male"); M_cleanDna.push_back(100); M_defectiveDna.push_back(0); M_generation.push_back(1); M_relationship.push_back("Self"); M_parentNames.push_back(std::make_pair(-1, -1)); M_beingLifespan.push_back(80); M_beingCurrentAge.push_back(1); M_hasMated.push_back(false); M_isOffspring.push_back(false); M_isCoupled.push_back(false); } for(int i = 0; i < maleCurrentPopulation; i++) { std::cout << "Name: " << M_beingNames[i] << "\n"; std::cout << "Gender: " << M_beingGenders[i] << "\n"; std::cout << "Clean DNA%: " << M_cleanDna[i] << "\n"; std::cout << "Defective DNA%: " << M_defectiveDna[i] << "\n"; std::cout << "Generation: " << M_generation[i] << "\n"; std::cout << "Relationship: " << M_relationship[i] << "\n"; std::cout << "Parents: " << M_parentNames[i].first << " " << M_parentNames[i].second << "\n"; std::cout << "Lifespan: " << M_beingLifespan[i] << "\n"; std::cout << "Current Age: " << M_beingCurrentAge[i] << "\n"; std::cout << "Has Mated: " << M_hasMated[i] << "\n"; std::cout << "Is Offspring: " << M_isOffspring[i] << "\n"; std::cout << "Is Coupled: " << M_isCoupled[i] << "\n\n"; } //Females for(int i = 0; i < femaleCurrentPopulation; i++) { F_beingNames.push_back(i); F_beingGenders.push_back("Female"); F_cleanDna.push_back(100); F_defectiveDna.push_back(0); F_generation.push_back(1); F_relationship.push_back("Self"); F_parentNames.push_back(std::make_pair(-1, -1)); F_beingLifespan.push_back(80); F_beingCurrentAge.push_back(1); F_hasMated.push_back(false); F_isOffspring.push_back(false); F_isCoupled.push_back(false); } for(int i = 0; i < femaleCurrentPopulation; i++) { std::cout << "Name: " << F_beingNames[i] << "\n"; std::cout << "Gender: " << F_beingGenders[i] << "\n"; std::cout << "Clean DNA%: " << F_cleanDna[i] << "\n"; std::cout << "Defective DNA%: " << F_defectiveDna[i] << "\n"; std::cout << "Generation: " << F_generation[i] << "\n"; std::cout << "Relationship: " << F_relationship[i] << "\n"; std::cout << "Parents: " << M_parentNames[i].first << " " << M_parentNames[i].second << "\n"; std::cout << "Lifespan: " << F_beingLifespan[i] << "\n"; std::cout << "Current Age: " << F_beingCurrentAge[i] << "\n"; std::cout << "Has Mated: " << F_hasMated[i] << "\n"; std::cout << "Is Offspring: " << F_isOffspring[i] << "\n"; std::cout << "Is Coupled: " << F_isCoupled[i] << "\n\n"; } /*TO DO //The rest of the program should pair up couples and create offspring, and apply any genetic penalties to them if need be. */ bool appLoopEnd = false; std::cout << "\n\n"; srand(time(NULL)); int maleName{}; int femaleName{}; while(!appLoopEnd) { for(int i = 0; i < 200; i++) { //Makes sure rand is seeded so we get a random male and female each time. maleName = rand() % maleCurrentPopulation; femaleName = rand() % femaleCurrentPopulation; //Check to see if there are any males or females that can be paired up. if either pop reaches 0, it will stop. if(maleCurrentPopulation > 0 && femaleCurrentPopulation > 0) { //Randomly choose pair of beings. pairBeingsUp.push_back(std::make_pair(maleName, femaleName)); if(pairBeingsUp[i].first != maleName && pairBeingsUp[i].second != femaleName) { maleCurrentPopulation--; femaleCurrentPopulation--; std::cout << pairBeingsUp[i].first << " " << pairBeingsUp[i].second << "\n"; } } } } return 0; }