samedi 5 août 2017

Return a random object from a list based on proprieties

This is quite a strange issue for me because I can't visualize my problem correctly. Just so that you know, I'm not really asking for code but just for an idea to write an approriate alogirthm that would generate some weather based on their probability of occuring.

Here's what I want to achieve :

Let's say I have a WeatherClass, with a parameter called "Probability". I want to have different weather instances with their own probability of "happening".

enum Probability {
Never = -1,
Low = 0,
Normal = 1,
Always = 2
};

std::vector<WeatherClass> WeatherContainer;

WeatherClass Sunny = WeatherClass();
Sunny.Probability = Probability.Normal;

WeatherClass Rainy = WeatherClass();
Rainy.Probability = Probability.Low;

WeatherClass Cloudy = WeatherClass();
Cloudy.Probability = Probability.Normal;

WeatherContainer.push_back(Sunny);
WeatherContainer.push_back(Rainy);
WeatherContainer.push_back(Cloudy);

Now, my question is : what is the most clever way to return some weather based on its own probability of happening?

I don't know why but I can't figure this out.. My first guess would be to have some kind of "luck" variable and compare it with the probability of each element or something similar.

Any hint or advice would be really helpful.

Greets,

required




Aucun commentaire:

Enregistrer un commentaire