Cześć wszystkim. Mam prosty kod, w którym mam klasę example
class Example
{
string name;
int chance;
public:
example(string name,int chance)
{
this ->name = name;
this ->chance = chance;
}
};
Teraz tworzę przykładowe 3 klasy od ręki w mainie i dodaje je wszystkie do vectora
int main()
{
Example ex1("example 1",25);
Example ex2("example 2",40);
Example ex3("example 3",5);
vector<Example> exampleList;
exampleList.push_back(ex1);
exampleList.push_back(ex2);
exampleList.push_back(ex3);
}
Chciałbym teraz stworzyć funkcję losującą pomiędzy trzema obiektami w liście z różną szansą na trafienie (jest też szansa 30% na to, że nie wylosujemy ani jednego obiektu). Niestety nie mam pojęcia jak to zrobić.
Stworzyłem coś takiego ale wydaje mi się, że może to być zrobione dużo prościej i lepiej
class Example
{
string name;
int chance;
public:
Example(string name,int chance)
{
this ->name = name;
this ->chance = chance;
}
int getChance()
{
return chance;
}
};
int draw(vector<Example> exampleList)
{
int fullChance = 0;
for(int i =0; i< exampleList.size(); ++i)
{
fullChance += exampleList[i].getChance();
}
int rest = 100 - fullChance;
srand(time(NULL));
int number = rand()%101;
fullChance = 0;
for(int i =0; i< exampleList.size(); ++i)
{
if(number > exampleList[0].getChance() + fullChance)
{
fullChance += exampleList[0].getChance();
}
else
{
return i;
}
}
return -1;
}
Example *decide(vector<Example> exampleList)
{
int i = draw(exampleList);
if(i == -1)
{
cout<<"Out of range"<<endl;
return NULL;
}
else
{
return &exampleList[i];
}
}
int main()
{
Example ex1("example 1",25);
Example ex2("example 2",40);
Example ex3("example 3",5);
vector<Example> exampleList;
exampleList.push_back(ex1);
exampleList.push_back(ex2);
exampleList.push_back(ex3);
Example *bufor = decide(exampleList);
if(bufor != NULL)
cout<<bufor->getChance()<<endl;
}
Jakieś porady jak to można usprawnić / zrobić lepiej?