C # Implementation of the winning algorithm,

Source: Internet
Author: User

C # Implementation of the winning algorithm,

Algorithm name Alias Method

public class AliasMethod {        /* The probability and alias tables. */        private int[] _alias;        private double[] _probability;        public AliasMethod(List<Double> probabilities) {            /* Allocate space for the probability and alias tables. */            _probability = new double[probabilities.Count];            _alias = new int[probabilities.Count];            /* Compute the average probability and cache it for later use. */            double average = 1.0 / probabilities.Count;            /* Create two stacks to act as worklists as we populate the tables. */            var small = new Stack<int>();            var large = new Stack<int>();            /* Populate the stacks with the input probabilities. */            for (int i = 0; i < probabilities.Count; ++i) {                /* If the probability is below the average probability, then we add                 * it to the small list; otherwise we add it to the large list.                 */                if (probabilities[i] >= average)                    large.Push(i);                else                    small.Push(i);            }            /* As a note: in the mathematical specification of the algorithm, we             * will always exhaust the small list before the big list.  However,             * due to floating point inaccuracies, this is not necessarily true.             * Consequently, this inner loop (which tries to pair small and large             * elements) will have to check that both lists aren't empty.             */            while (small.Count > 0 && large.Count > 0) {                /* Get the index of the small and the large probabilities. */                int less = small.Pop();                int more = large.Pop();                /* These probabilities have not yet been scaled up to be such that                 * 1/n is given weight 1.0.  We do this here instead.                 */                _probability[less] = probabilities[less] * probabilities.Count;                _alias[less] = more;                /* Decrease the probability of the larger one by the appropriate                 * amount.                 */                probabilities[more] = (probabilities[more] + probabilities[less] - average);                /* If the new probability is less than the average, add it into the                 * small list; otherwise add it to the large list.                 */                if (probabilities[more] >= average)                    large.Push(more);                else                    small.Push(more);            }            /* At this point, everything is in one list, which means that the             * remaining probabilities should all be 1/n.  Based on this, set them             * appropriately.  Due to numerical issues, we can't be sure which             * stack will hold the entries, so we empty both.             */            while (small.Count > 0)                _probability[small.Pop()] = 1.0;            while (large.Count > 0)                _probability[large.Pop()] = 1.0;        }        /**         * Samples a value from the underlying distribution.         *         * @return A random value sampled from the underlying distribution.         */        public int next() {            long tick = DateTime.Now.Ticks;            var seed = ((int)(tick & 0xffffffffL) | (int)(tick >> 32));            unchecked {                seed = (seed + Guid.NewGuid().GetHashCode() + new Random().Next(0, 100));            }            var random = new Random(seed);            int column = random.Next(_probability.Length);            /* Generate a biased coin toss to determine which option to pick. */            bool coinToss = random.NextDouble() < _probability[column];            return coinToss ? column : _alias[column];        }    }

Test

Dictionary <String, Double> map = new Dictionary <String, Double> (); map. add ("1 gold coin", 0.2); map. add ("2 gold coins", 0.15); map. add ("3 gold coins", 0.1); map. add ("4 gold coins", 0.05); map. add ("not winning", 0.5); List <Double> list = new List <Double> (map. values); List <String> gifts = new List <String> (map. keys); AliasMethod method = new AliasMethod (list); Dictionary <String, int> resultMap = new Dictionary <String, int> (); for (int I = 0; I <10; I ++) {int index = method. next (); string key = gifts [index]; Console. writeLine (index + ":" + key );}

Finally, we recommend that you use www.ahjesus.com for free for mobile phone numbers from all over the world.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.