001package net.minecraft.util;
002
003import java.util.Collection;
004import java.util.Iterator;
005import java.util.Random;
006
007public class WeightedRandom
008{
009    /**
010     * Returns the total weight of all items in a collection.
011     */
012    public static int getTotalWeight(Collection par0Collection)
013    {
014        int i = 0;
015        WeightedRandomItem weightedrandomitem;
016
017        for (Iterator iterator = par0Collection.iterator(); iterator.hasNext(); i += weightedrandomitem.itemWeight)
018        {
019            weightedrandomitem = (WeightedRandomItem)iterator.next();
020        }
021
022        return i;
023    }
024
025    /**
026     * Returns a random choice from the input items, with a total weight value.
027     */
028    public static WeightedRandomItem getRandomItem(Random par0Random, Collection par1Collection, int par2)
029    {
030        if (par2 <= 0)
031        {
032            throw new IllegalArgumentException();
033        }
034        else
035        {
036            int j = par0Random.nextInt(par2);
037            Iterator iterator = par1Collection.iterator();
038            WeightedRandomItem weightedrandomitem;
039
040            do
041            {
042                if (!iterator.hasNext())
043                {
044                    return null;
045                }
046
047                weightedrandomitem = (WeightedRandomItem)iterator.next();
048                j -= weightedrandomitem.itemWeight;
049            }
050            while (j >= 0);
051
052            return weightedrandomitem;
053        }
054    }
055
056    /**
057     * Returns a random choice from the input items.
058     */
059    public static WeightedRandomItem getRandomItem(Random par0Random, Collection par1Collection)
060    {
061        return getRandomItem(par0Random, par1Collection, getTotalWeight(par1Collection));
062    }
063
064    /**
065     * Returns the total weight of all items in a array.
066     */
067    public static int getTotalWeight(WeightedRandomItem[] par0ArrayOfWeightedRandomItem)
068    {
069        int i = 0;
070        WeightedRandomItem[] aweightedrandomitem1 = par0ArrayOfWeightedRandomItem;
071        int j = par0ArrayOfWeightedRandomItem.length;
072
073        for (int k = 0; k < j; ++k)
074        {
075            WeightedRandomItem weightedrandomitem = aweightedrandomitem1[k];
076            i += weightedrandomitem.itemWeight;
077        }
078
079        return i;
080    }
081
082    /**
083     * Returns a random choice from the input array of items, with a total weight value.
084     */
085    public static WeightedRandomItem getRandomItem(Random par0Random, WeightedRandomItem[] par1ArrayOfWeightedRandomItem, int par2)
086    {
087        if (par2 <= 0)
088        {
089            throw new IllegalArgumentException();
090        }
091        else
092        {
093            int j = par0Random.nextInt(par2);
094            WeightedRandomItem[] aweightedrandomitem1 = par1ArrayOfWeightedRandomItem;
095            int k = par1ArrayOfWeightedRandomItem.length;
096
097            for (int l = 0; l < k; ++l)
098            {
099                WeightedRandomItem weightedrandomitem = aweightedrandomitem1[l];
100                j -= weightedrandomitem.itemWeight;
101
102                if (j < 0)
103                {
104                    return weightedrandomitem;
105                }
106            }
107
108            return null;
109        }
110    }
111
112    /**
113     * Returns a random choice from the input items.
114     */
115    public static WeightedRandomItem getRandomItem(Random par0Random, WeightedRandomItem[] par1ArrayOfWeightedRandomItem)
116    {
117        return getRandomItem(par0Random, par1ArrayOfWeightedRandomItem, getTotalWeight(par1ArrayOfWeightedRandomItem));
118    }
119}