Pass a set of conditions and weights, and build a random condition generator.
When serving fruit...
- Serve
Apples50% of the time - Serve
Oranges10% of the time - Serve
Grapesthe rest of the time - Serve
Jackfruitnever
require_once 'class-random-weighted-conditions.php';
$conditions = array(
'apples' => 50,
'oranges' => 10,
'grapes' => null,
`jackfruit` => 0,
);
$colors = new \WPAZ_RWC\V_1_2\Random_Weighted_Conditions( $conditions );
// will be `apples` 50%, `oranges` 10% and `grapes` 40%
echo $colors->get_random_condition();Developer provides array() of key => val pairs with condition keys and weight values.
- 0-100 are treated as percentage weights. 0 completely excludes a condition.
- All other true-testing
empty()values are giving equal distribution of remaining total, following preassigned weights
Class validates data and creates an array with 100 string conditions (total can be modified, conditions looped sequentially), based on weights provided.
Class randomly selects one of strings.
💃