Advertisement
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Get a Random Number out of an Array?

Comments in 'Plugin Development' started by Samueljh1, Jan 24, 2015.

  1. Samueljh1
    Offline

    Samueljh1 Active Member Plugin Developer

    Joined:
    Jan 9, 2015
    Posts:
    316
    Plugins:
    5
    Minecraft User:
    Samueljh1_
    Hi, I basically want to get a random number, but I want this number to be in an array.

    Basically...

    PHP:
    $ids = [3,5,27,53,87]

    Then, I need to get a random number that is one of the ints in that array..

    Also, if you can tell me, how can I make an inventory array with lots of random numbers to put in a chest?

    So I have an array with the ids and data... I need help putting that in a big array.. (Separate for ids and data), then convert those 2 arrays in a inventory format so I can use setInventory() with that!

    Thanks in advance!! :)
  2. Falk
    Offline

    Falk Staff Member Global Moderator

    Joined:
    Sep 2, 2013
    Posts:
    1,707
    Plugins:
    22
    Minecraft User:
    Falkirknh
    PHP:
    $id $ids[array_rand($ids)];
    I don't have time to write out the second thing.
  3. Samueljh1
    Offline

    Samueljh1 Active Member Plugin Developer

    Joined:
    Jan 9, 2015
    Posts:
    316
    Plugins:
    5
    Minecraft User:
    Samueljh1_
    OK ty!
    OK ty! I'm not sure how to convert to inventory format though! :(
  4. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,306
    Plugins:
    11
    Minecraft User:
    PEMapModder
    What is inventory format?
  5. 64FF00
    Offline

    64FF00 Active Member Plugin Developer

    Joined:
    May 11, 2014
    Posts:
    298
    Plugins:
    2
    Minecraft User:
    64FF00
    http://php.net/manual/en/function.array-rand.php
    PEMapModder likes this.
  6. 64FF00
    Offline

    64FF00 Active Member Plugin Developer

    Joined:
    May 11, 2014
    Posts:
    298
    Plugins:
    2
    Minecraft User:
    64FF00
    Btw...
  7. Falk
    Offline

    Falk Staff Member Global Moderator

    Joined:
    Sep 2, 2013
    Posts:
    1,707
    Plugins:
    22
    Minecraft User:
    Falkirknh
    That doesn't matter much.
  8. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,306
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Personally I like this more because it is faster.
    PHP:
    function random_array_value(array $array){
        return 
    $array[array_keys($array)[mt_rand(0count($array))]];
    }
  9. Samueljh1
    Offline

    Samueljh1 Active Member Plugin Developer

    Joined:
    Jan 9, 2015
    Posts:
    316
    Plugins:
    5
    Minecraft User:
    Samueljh1_
    Basically, you know when you do
    PHP:
    $player->getInventory()->getContents(); 
    That returns an array, that is in a format that allows to to do
    PHP:
    $player>getInventory()->setContents();
  10. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,306
    Plugins:
    11
    Minecraft User:
    PEMapModder
    It's not called "format". It is called data type :)

    Use this:
    PHP:
    $id $ids[mt_rand(0count($ids)];
    $inventory->addItem(Item::get($id));
  11. Samueljh1
    Offline

    Samueljh1 Active Member Plugin Developer

    Joined:
    Jan 9, 2015
    Posts:
    316
    Plugins:
    5
    Minecraft User:
    Samueljh1_
    OK, but is there a way to do it like that? I want the items to be in random slots :)
  12. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,306
    Plugins:
    11
    Minecraft User:
    PEMapModder
    PHP:
    $inventory->setItem(mt_rand(0$inventory->getSize() - 1), $item);
  13. Samueljh1
    Offline

    Samueljh1 Active Member Plugin Developer

    Joined:
    Jan 9, 2015
    Posts:
    316
    Plugins:
    5
    Minecraft User:
    Samueljh1_
    OK thanks.

Share This Page

Advertisement