Last active
March 27, 2022 17:39
-
-
Save duplaja/24812d04d899c107477331bbb859ac40 to your computer and use it in GitHub Desktop.
Weighted "Spin the Prize Wheel" function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function lottery_spin_the_wheel() { | |
//Form is key (reward as string) value (frequency as int) | |
$rewards_master = array( | |
'1 bonus token'=>25, | |
'5-album mini themed list'=>20, | |
'Album on demand'=>20, | |
'Trade 1 token for 2 tokens of your choice'=>15, | |
'Top-off overload +5 on Wednesday'=>10, | |
'Book recommendation'=>10, | |
'Described movie recommendation'=>10, | |
'Top-off overload +10 on Wednesday'=>8, | |
'3 albums on demand'=>8, | |
'2 bonus tokens'=>8, | |
'1 surprise item'=>8, | |
'Snack delivery'=>8, | |
'Trade 3 tokens for 5 tokens of your choice'=>8, | |
'10-album themed list'=>5, | |
'Ice cream date'=>3, | |
'Home-cooked meal of your choice'=>3, | |
'Themed list sample platter: 3 5-album themed lists'=>3, | |
'3 surprise items'=>3, | |
'Instant top-off on any day'=>3, | |
'Dinner date'=>3, | |
'5 bonus tokens'=>2, | |
'Themed list party pack: 2 10-album themed lists'=>1, | |
'5 surprise items'=>1, | |
'25-album themed list'=>1 | |
); | |
$rewards_choices = array(); | |
foreach ($rewards_master as $reward => $frequency) { | |
$rewards_choices = array_merge($rewards_choices, array_fill(0, $frequency, "$reward")); | |
} | |
shuffle($rewards_choices); | |
$reward = ucwords($rewards_choices[0]); | |
return $reward; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Suggestion: I have a google sheet to build out the $rewards_master array. One column is frequency, one is reward string, and I use CONCATENATE() to build out the array elements, as well as calculate percentages.
In the above, the most frequent item (1 bonus token) happens roughly 13.4% of the time, while the least frequent (value of 1) happen 0.54% of the time.