problem with the probability calculations?

Discussion in 'Development' started by Hoyee, Mar 9, 2020.

  1. Hoyee

    Hoyee Slime

    Messages:
    90
    PHP:
      $rand mt_rand(1,100);
    if(
    $rand<=50){
      
    $player->getInventory()->addItem(Item::get(264,0,1));
    } else if(
    $rand<=20){
    $player->getInventory()->addItem(Item::get(368,0,1));
    } else if(
    $rand<=30){
    $player->getInventory()->addItem(Item::get(265,0,1));
    }
    When I use this code, It only works in ItemCode 264(diamond). why? I want to make if 50%, give diamond, if 20%, give ender, if 30%, give iron
     
  2. HimbeersaftLP

    HimbeersaftLP Fish

    Messages:
    2,362
    GitHub:
    HimbeersaftLP
    You will need to put the smallest number first, because with your code, even if $rand is smaller than 20, the condition <=50 is still true and it will use that.
     
    GamakCZ likes this.
  3. Hoyee

    Hoyee Slime

    Messages:
    90
    I changed what you said, but when I tested, it sometimes give nothing. why this problem happening?

    PHP:
    if($rand<=20){
      
    $player->getInventory()->addItem(Item::get(265,0,1));
    } else if(
    $rand<=30){
    $player->getInventory()->addItem(Item::get(368,0,1));
    } else if(
    $rand<=50){
    $player->getInventory()->addItem(Item::get(264,0,1));
    }
     
  4. HimbeersaftLP

    HimbeersaftLP Fish

    Messages:
    2,362
    GitHub:
    HimbeersaftLP
    Did you remove the $rand = mt_rand(1,100); ?
     
  5. Hoyee

    Hoyee Slime

    Messages:
    90
    PHP:
    $rand mt_rand(1,100);

    if(
    $rand<=20){
      
    $player->getInventory()->addItem(Item::get(265,0,1));
    } else if(
    $rand<=30){
    $player->getInventory()->addItem(Item::get(368,0,1));
    } else if(
    $rand<=50){
    $player->getInventory()->addItem(Item::get(264,0,1));
    }
    NO
     
  6. HimbeersaftLP

    HimbeersaftLP Fish

    Messages:
    2,362
    GitHub:
    HimbeersaftLP
    That's odd, are you sure the code is getting executed?
     
  7. Hoyee

    Hoyee Slime

    Messages:
    90
    what mean?? It works well, everything is good but a little more frequently, they just take the stones and don't give the items. what problem is this? I need to solve this problem quickly
     
    Last edited: Mar 9, 2020
  8. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,979
    GitHub:
    sof3
    $rand is a random number between 0 and 100. Try reading your code again. Which lines of code would be executed if $rand is 90?
     
    GamakCZ and HimbeersaftLP like this.
  9. Hoyee

    Hoyee Slime

    Messages:
    90
    than, How can I make probability?
     
  10. Hoyee

    Hoyee Slime

    Messages:
    90
    PHP:
     $rand mt_rand(1,100);
    if(
    $rand<=40){
      
    $player->getInventory()->addItem(Item::get(264,0,1));
    } else if(
    $rand<=80){
    $player->getInventory()->addItem(Item::get(368,0,1));
    } else if(
    $rand<=100){
    $player->getInventory()->addItem(Item::get(265,0,1));
    }
    is this right? 264 's probability is 40%, 368's probability is 80%, and 265's probability is 100%
     
  11. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,979
    GitHub:
    sof3
    No, it is 40% 40% 20%, because $rand cannot be <= 40 if the first condition is wrong.

    If $rand is 0-40, you get first. If $rand is 41-80, you get second. If $rand is 81-99, you get third.
     
    GamakCZ and HimbeersaftLP like this.
  12. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,979
    GitHub:
    sof3
    Actually there is an off by one issue. The real probability in your code is 41% 40% 19% because $rand can be 0 but cannot be 100 (at most 99).
     
  13. HimbeersaftLP

    HimbeersaftLP Fish

    Messages:
    2,362
    GitHub:
    HimbeersaftLP
    Are you sure?
    https://www.php.net/manual/en/function.mt-rand.php
     
  14. Hoyee

    Hoyee Slime

    Messages:
    90
    than, is this right?
    PHP:
    $rand mt_rand(1,15)

    if(
    $rand<=5){
    sendMessage(probability is 5%);
    } else if(
    $rand<=15){
    sendMessage(probability is 10%);
    }
     
  15. GamakCZ

    GamakCZ Zombie Pigman

    Messages:
    495
    GitHub:
    GamakCZ
    Nope. In first case it's 33.3% and in the second one 66.6%
     
  16. TwistedAsylumMC

    TwistedAsylumMC Slime

    Messages:
    96
    GitHub:
    twistedasylummc
    To work out the probability you can use this formula:
    upload_2020-3-10_22-13-36.png
     
    HimbeersaftLP likes this.
  17. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,979
    GitHub:
    sof3
    HimbeersaftLP likes this.

Share This Page

  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.