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

giving players items with chances

Discussion in 'Development' started by Levi, Jun 17, 2018.

  1. Levi

    Levi Skeleton

    Messages:
    955
    GitHub:
    captainleviftw
    I want to make players get 20% chance of getting diamonds and 80% chance of getting dirt. but i tried
    PHP:
    rand(1,100);
    if(
    $rand =< 20){
    20%
    }elseif(
    $rand 20){
    80%
    }
    but they said this one is bad
     
  2. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    This has a 1% (yeah, no "pseudo random") chance of returning any number between 1 and 100.
    PHP:
    $rand rand(1100);
    So this will have a 20% chance of returning true.
    PHP:
    if($rand <= 20){
        return 
    true;
    }
    return 
    false;
     
  3. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    What you can do is recalculate a random number every time. That makes it a little more random.
    PHP:
    if(rand(1100) <= 20){
    }elseif(
    rand(1100) <= 80){
    }
     
  4. eDroid

    eDroid Witch

    Messages:
    59
    GitHub:
    edroiid
    PHP:
    function chance(int $chance, callable $high, callable $low){
        
    $rand rand(1100);
        return 
    $rand $chance $low() : $high();
    }

    chance(20, function(){
        echo 
    "give the players diamonds";
    }, function(){
        echo 
    "give em dirt";
    });
     
    Levi likes this.
  5. byyEmirhanWSD

    byyEmirhanWSD Witch

    Messages:
    50
    GitHub:
    EmirhanWSD
    PHP:
    $chance rand(02);
    switch(
    $chance) {
      case 
    0:

      break;
      case 
    1:

      break;
      case 
    2:

      break;

    }
     
  6. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    That would be 33.3% chance.
    20 and 80 percent would be rand(1, 5) so each number has a 20% chance of being called (20% = rand(1, 5) === 1, 80% = rand(1, 5) !== 1).
     
  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.