https://github.com/wordpress-phoenix/random-weighted-conditions
Lightweight PHP class for simplistic weighting of string conditions
https://github.com/wordpress-phoenix/random-weighted-conditions
math php probability randomizer wordpress
Last synced: about 2 months ago
JSON representation
Lightweight PHP class for simplistic weighting of string conditions
- Host: GitHub
- URL: https://github.com/wordpress-phoenix/random-weighted-conditions
- Owner: WordPress-Phoenix
- License: gpl-2.0
- Created: 2017-08-03T01:01:26.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2017-09-13T22:34:16.000Z (almost 9 years ago)
- Last Synced: 2025-10-13T04:36:00.645Z (9 months ago)
- Topics: math, php, probability, randomizer, wordpress
- Language: PHP
- Homepage:
- Size: 15.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
Awesome Lists containing this project
README
# Random Weighted Conditions
Pass a set of conditions and weights, and build a random condition generator.
#### i.e.
When serving fruit...
* Serve `Apples` 50% of the time
* Serve `Oranges` 10% of the time
* Serve `Grapes` the rest of the time
* Serve `Jackfruit` never
# Example
```php
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();
```
# How it Works
### Step 1
Developer provides `array()` of `key => val` pairs with condition keys and weight values.
##### Possible 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_
### Step 2
Class validates data and creates an array with 100 string conditions (total can be modified, conditions looped sequentially), based on weights provided.
### Step 3
Class randomly selects one of strings.
:dancer: