Hey, I am currently making a crate plugin that when you hit the chest with a key it gives you 3 random items I managed to do the crate and key part but adding percentages to winning an item I couldn't make that. example 3% chance of winning diamond boots 5% chance of winning diamond helmet 3% chance of winning diamond sword And so on. Any examples or links are greatly appreciated.
https://www.gamasutra.com/blogs/DanielCook/20141215/232300/Loot_drop_best_practices.php https://gamedev.stackexchange.com/questions/140979/modern-loot-table-concepts https://stackoverflow.com/questions/59268542/picking-random-item-from-php-array-based-on-probability https://www.geeksforgeeks.org/how-to-get-weighted-random-choice-in-python/
What about this one PHP: // The array passed to the function should be your $entries arrayfunction randProb(array $items) { $totalProbability = 0; // This is defined to keep track of the total amount of entries foreach ($items as $item => $probability) { $totalProbability += $probability; } $stopAt = rand(0, $totalProbability); // This picks a random entry to select $currentProbability = 0; // The current entry count, when this reaches $stopAt the winner is chosen foreach ($items as $item => $probability) { // Go through each possible item $currentProbability += $probability; // Add the probability to our $currentProbability tracker if ($currentProbability >= $stopAt) { // When we reach the $stopAt variable, we have found our winner return $item; } } return null;} https://www.tehplayground.com/YC75TB5OWj1uJykZ
PHP: public function openPremium(){ $items = [ "Sword" => 3, "Money" => 94, "Diamond" => 3 ]; $totalProbability = 100; foreach ($items as $item => $probability){ $totalProbability += $probability; } $stopAt = rand(0, $totalProbability); $currentProbability = 0; foreach ($items as $item => $probability){ $currentProbability += $probability; if ($currentProbability >= $stopAt) { echo "$item\n"; return true; } } echo "null\n"; return false;} When i run that code it returns Money Money null Money Money Money null null Money null any idea on why it's returning null
Don't preset the totalProbability variable. There are literally few lines of logic, how can you be so lazy to read through and come to conclusion yourself?