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

Solved Error with mt_rand, help me please

Discussion in 'Help' started by DiamondGamer30, Nov 10, 2017.

  1. DiamondGamer30

    DiamondGamer30 Baby Zombie

    Messages:
    175
    GitHub:
    diamondgamermcpe
    When I was working on my relic plugin, you were able to get a relic in every stone you mine but I'm trying to make it in every 50 blocks, is that possible?
    PHP:
    public function onBreak(BlockBreakEvent $event) {
        
        if(
    $event->getBlock()->getId() == && mt_rand(150)) {
        
            
    $player $event->getPlayer();
            
    $name $player->getName();
        
            
    $tier1 Item::get(Item::NETHER_STAR01);
            
    $tier1->setCustomName(TF::RESET TF::GOLD TF::BOLD "Ancient Relic" TF::RESET PHP_EOL .
            
    TF::GRAY " * An ancient relic found by mining" PHP_EOL .
            
    TF::GRAY " * Tap anywhere to see what it holds");

            
    $player->getInventory()->addItem($tier1);
     
  2. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    You can change mt_rand(1, 50) to mt_rand(-49, 1) since negative numbers and zeroes are equivalent to false.
    Or
    a cleaner method...
    PHP:
    if(mt_rand(150) === 1){
        
    //mt_rand(1, 50) generates a rand number between 1 and 50.
        //There's 1 in a 50 chance (ignoring "psuedo" factors) that mt_rand(1, 50)
        //will return 1.
    }
     
    Last edited: Nov 11, 2017
    armagadon159753 likes this.
  3. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    facepalm section
     
    Muqsit and Thunder33345 like this.
  4. eDroid

    eDroid Witch

    Messages:
    59
    GitHub:
    edroiid
    who even uses mt_rand anymore?
     
  5. DiamondGamer30

    DiamondGamer30 Baby Zombie

    Messages:
    175
    GitHub:
    diamondgamermcpe
    I do, idk whats the new thing.
     
  6. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    wait.. what do the cool kids use then? :eek:
     
    jasonwynn10 likes this.
  7. armagadon159753

    armagadon159753 Zombie

    Messages:
    217
    GitHub:
    armagadon159753
    rand anyway ^~^
     
  8. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    rand is insecure
    we need something more secure then rand!!!
     
    armagadon159753 likes this.
  9. armagadon159753

    armagadon159753 Zombie

    Messages:
    217
    GitHub:
    armagadon159753
    I said that to joke but it is good to remind him
     
  10. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    Not true. Negative numbers are resolved as true.
     
  11. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    ah, my life has been a life for a long time.

    It is not secure but it doesn't matter in this case
     
  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.