1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

Add percentage for win

Discussion in 'Development' started by Besher, May 15, 2021.

  1. Besher

    Besher Witch

    Messages:
    51
    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.
     
  2. Primus

    Primus Zombie Pigman

    Messages:
    749
  3. Besher

    Besher Witch

    Messages:
    51
  4. Primus

    Primus Zombie Pigman

    Messages:
    749
    What about this one
    PHP:
    // The array passed to the function should be your $entries array
    function 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
     
    Last edited: May 15, 2021
  5. Besher

    Besher Witch

    Messages:
    51
    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
     
  6. Primus

    Primus Zombie Pigman

    Messages:
    749
    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?
     
  7. Besher

    Besher Witch

    Messages:
    51
    One last question how would I set an item as a key in array cus i can't set an object as key
     
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.