How can I make probability

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

  1. Hoyee

    Hoyee Baby Zombie

    Messages:
    126
    how can I make it?

    some people says can make with mt_rand but I think it doesnt work well

    PHP:
     $rand mt_rand(1,100);
    if(
    $rand<=10){
      
    $player->getInventory()->addItem(Item::get(264,0,1));
    } else if(
    $rand<=40){
    $player->getInventory()->addItem(Item::get(368,0,1));
    } else if(
    $rand<=50){
    $player->getInventory()->addItem(Item::get(265,0,1));
    }
    I want to make 10%, 40%, 50%
     
  2. Mr174

    Mr174 Baby Zombie

    Messages:
    187
    GitHub:
    mr174
    Thats right
     
  3. Hoyee

    Hoyee Baby Zombie

    Messages:
    126
    but how about when mt_rand get's 80 value?
    It will not work. so I want real probability
     
  4. HimbeersaftLP

    HimbeersaftLP Fish

    Messages:
    2,400
    GitHub:
    HimbeersaftLP
    PHP:
    $rand mt_rand(1,100);
    if (
    $rand <= 10) { // 10% chance
      
    $player->getInventory()->addItem(Item::get(264,0,1));
    } else if(
    $rand <= 50) { // 40% chance
      
    $player->getInventory()->addItem(Item::get(368,0,1));
    } else { 
    // 50% chance
      
    $player->getInventory()->addItem(Item::get(265,0,1));
    }
     
    Muqsit likes this.
  5. Hoyee

    Hoyee Baby Zombie

    Messages:
    126
    than it can also over 100? it can? and I need to use just else in last?
    Code:
    $rand = mt_rand(1,140);
    if ($rand <= 10) { // 10% chance
      $player->getInventory()->addItem(Item::get(264,0,1));
    } else if($rand <= 50) { // 40% chance
      $player->getInventory()->addItem(Item::get(368,0,1));
    } else if($rand <= 140){ // 90% chance
      $player->getInventory()->addItem(Item::get(265,0,1));
    }
     
  6. TwistedAsylumMC

    TwistedAsylumMC Slime

    Messages:
    96
    GitHub:
    twistedasylummc
    You can do that but you can replace the last "else if" with just "else" because that if statement will always be true if it ever gets called. Also since you changed the maximum number, the percentages you have are now incorrect.
    10/140 * 100 = 7.14% (to 2 d.p)
    40/140 * 100 = 28.57% (to 2 d.p)
    100/140 * 100 = 71.43% (to 2 d.p)
     
  7. Muqsit

    Muqsit Chicken

    Messages:
    1,549
    GitHub:
    muqsit
    It's supposed to be <= 40 for 40% chance.

    The 140? That's going to make your stuff more complicated.

    mt_rand(1, 100) gives you a number in the range [1, 100].
    The probability of the number being <= 30 is 30%.
    The probability of the number being <= x is x% (x belongs in [1, 100]).

    If you change the range to [1, 140], the probability changes as well.

    If you need precision, go for (mt_rand() / mt_getrandmax()) * 100.

    Something to note: reusing the same probability value changes the probability too.
     
    Fadhel 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.