Solved Randomising Lists

Discussion in 'Plugin Development' started by Coopah, Apr 15, 2015.

Thread Status:
Not open for further replies.
  1. Offline

    Coopah

    I've created a few lists which contain numerous amounts of items and I'm looking for a way to pick one of these lists randomly.
    This is my setup for one of the lists (keep in mind there is multiple lists):
    Code:
                    ItemStack[] itemStacks1 = new ItemStack[]{new ItemStack(Material.DIAMOND, 5),new ItemStack(Material.DIAMOND_SWORD), new ItemStack(Material.DIAMOND_AXE), new ItemStack(Material.TNT, 4)};
    
    So, I'm trying to randomly pick one of these lists. How would I do it? I've created a Random but don't understand how to pick a list.
     
  2. Offline

    nverdier

    @Coopah Get a random int with a range of the length of the array. Then get the ItemStack at the random index.
     
  3. Offline

    Coopah

    @nverdier
    The lists aren't all the same size item wise.
     
  4. Offline

    nverdier

    @Coopah Ohhhh wait there's multiple arrays and you want to select a random one? Just have a List of them, and randomly select one of the arrays from it.
     
  5. Offline

    Coopah

    @nverdier What list type would I use and how would I add them? Use a arraylist and add them through their names?
     
  6. Offline

    undeaD_D

    Code:
            /*
             * import java.util.ArrayList;
             * import java.util.Random;
             * import org.bukkit.Material;
             * import org.bukkit.inventory.ItemStack;
             */
          
          
             ArrayList<ItemStack[]> list = new ArrayList<ItemStack[]>();
           
             ItemStack[] itemStacks1 = new ItemStack[]{new ItemStack(Material.DIAMOND, 5),new ItemStack(Material.DIAMOND_SWORD), new ItemStack(Material.DIAMOND_AXE), new ItemStack(Material.TNT, 4)};
             list.add(itemStacks1);
             ItemStack[] itemStacks2 = new ItemStack[]{new ItemStack(Material.DIAMOND, 5),new ItemStack(Material.DIAMOND_SWORD), new ItemStack(Material.DIAMOND_AXE), new ItemStack(Material.TNT, 4)};
             list.add(itemStacks2);
             ItemStack[] itemStacks3 = new ItemStack[]{new ItemStack(Material.DIAMOND, 5),new ItemStack(Material.DIAMOND_SWORD), new ItemStack(Material.DIAMOND_AXE), new ItemStack(Material.TNT, 4)};
             list.add(itemStacks3);
             //...
             ItemStack[] itemStacksn = new ItemStack[]{new ItemStack(Material.DIAMOND, 5),new ItemStack(Material.DIAMOND_SWORD), new ItemStack(Material.DIAMOND_AXE), new ItemStack(Material.TNT, 4)};
             list.add(itemStacksn);
    
             Random randomGenerator = new Random();
             int i = randomGenerator.nextInt(list.size());
           
             ItemStack[] rand = list.get(i);
     
    Coopah likes this.
  7. Offline

    Coopah

    undeaD_D likes this.
Thread Status:
Not open for further replies.

Share This Page