-
Notifications
You must be signed in to change notification settings - Fork 12
Handling Probabilities
IMPORTANT: every percents in Zara are measured in 0-100 int scale!
(except the rain intensity value that is in 0..1
scale)
Very often you need to trigger some event according to its probability of happening. For this purpose WillHappen extension method for int type exists. It takes one argument -- probability percent (int, 0 to 100) and returns bool.
For example,
if (50.WillHappen()) {
...
means that condition is satisfied approximately in every 50% of calls.
Try not to bury probabilities deep inside your methods, use class-level constants for this. Good example of the "right" way of using probabilities -- is HealthController class.
Example:
private const int ProbabilityOfRain = 60;
and then somewhere in some method:
if (ProbabilityOfRain.WillHappen()) {
...
0.WillHappen()
will always return false.
100.WillHappen()
will always return true.
This WillHappen
method calls Zara's randomization function that was set during the engine set up.