    001    package net.minecraft.src;
    002
    003    import java.util.Collection;
    004    import java.util.Iterator;
    005    import java.util.Random;
    006
    007    public 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 var1 = 0;
    015            WeightedRandomItem var3;
    016
    017            for (Iterator var2 = par0Collection.iterator(); var2.hasNext(); var1 += var3.itemWeight)
    018            {
    019                var3 = (WeightedRandomItem)var2.next();
    020            }
    021
    022            return var1;
    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 var3 = par0Random.nextInt(par2);
    037                Iterator var4 = par1Collection.iterator();
    038                WeightedRandomItem var5;
    039
    040                do
    041                {
    042                    if (!var4.hasNext())
    043                    {
    044                        return null;
    045                    }
    046
    047                    var5 = (WeightedRandomItem)var4.next();
    048                    var3 -= var5.itemWeight;
    049                }
    050                while (var3 >= 0);
    051
    052                return var5;
    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 var1 = 0;
    070            WeightedRandomItem[] var2 = par0ArrayOfWeightedRandomItem;
    071            int var3 = par0ArrayOfWeightedRandomItem.length;
    072
    073            for (int var4 = 0; var4 < var3; ++var4)
    074            {
    075                WeightedRandomItem var5 = var2[var4];
    076                var1 += var5.itemWeight;
    077            }
    078
    079            return var1;
    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 var3 = par0Random.nextInt(par2);
    094                WeightedRandomItem[] var4 = par1ArrayOfWeightedRandomItem;
    095                int var5 = par1ArrayOfWeightedRandomItem.length;
    096
    097                for (int var6 = 0; var6 < var5; ++var6)
    098                {
    099                    WeightedRandomItem var7 = var4[var6];
    100                    var3 -= var7.itemWeight;
    101
    102                    if (var3 < 0)
    103                    {
    104                        return var7;
    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    }