Lottery algorithm and lottery probability algorithm

Source: Internet
Author: User
Tags random seed

Lottery algorithm and lottery probability algorithm

I am not satisfied with the lottery code I found on the Internet. If I need it, I can use it directly without the copyright fee.

/// <Summary> /// Lucky Draw /// </summary> public class Prize {// <summary> // Prize keyword /// </summary> public string Key {get; set ;}//< summary> /// weight/quantity /// </summary> public int Poll {get; set ;} /// <summary> /// winning range /// </summary> class Area {// <summary> // prize keyword /// </summary> public string Key {get; set ;}//< summary> /// Start index location /// </summary> public int Start {get; set ;}/// <summary>/ // End index position // </summary> public int Over {get; set ;}} /// <summary> /// Random Seed /// </summary> static Random Rand = new Random (int) DateTime. now. ticks); /// <summary> // The Lottery Draw and weight (the size of the area occupied by the wheel) is the chance of winning the prize /// </summary> /// <param name = "prizeList"> gift list (if it is not a winning prize, leave the wheel blank and add it to the list) </param> // <returns> </returns> public static string Roulette (List <Prize> prizeList) {if (prizeList = null | prizeList. count = 0) Return string. empty; if (prizeList. any (x => x. poll <1) throw new ArgumentOutOfRangeException ("poll weight cannot be less than 1"); if (prizeList. count = 1) return prizeList [0]. key; // only one gift Int32 total = prizeList. sum (x => x. poll); // weight and if (total> 1000) throw new ArgumentOutOfRangeException ("poll weight and cannot exceed 1000"); // array storage space restrictions. A maximum of one thousand prizes (and each prize has a weight of 1) List <int> speed = new List <int> (); // Random Seed for (int I = 0; I <total; I ++) speed. add (I); int pos = 0; Dictionary <int, string> box = new Dictionary <int, string> (); foreach (Prize p in prizeList) {for (int c = 0; c <p. poll; c ++) // The larger the weight, the more parts it occupies. {pos = Prize. rand. next (speed. count); // obtain the Random Seed coordinate box [speed [pos] = p. key; // random gifts are indexed in the box of speed [pos. removeAt (pos); // remove the extracted box Index No.} return box [Prize. rand. next (total)] ;}/// <summary> // Prize box lottery. Each participant corresponds to a prize box, prize box as many people participate /// </summary> /// <param name = "prizeList"> gift list </param> /// <param name =" peopleCount "> participants </param> // <returns> </returns> public static string LunkyBox (List <Prize> prizeList, int peopleCount) {if (prizeList = null | prizeList. count = 0) return string. empty; if (prizeList. any (x => x. poll <1) throw new ArgumentOutOfRangeException ("the number of poll gifts cannot be less than 1"); if (lelecount <1) throw new ArgumentOutOfRangeException ("the number of parameters cannot be less than 1"); if (prizeList. count = 1 & peopleCount <= prizeList [0]. poll) return prizeList [0]. key; // there is only one type of gift and the number of gifts is greater than or equal to the number of participants int pos = 0; List <Area> box = new List <Area> (); foreach (Prize p in prizeList) {box. add (new Area () {Key = p. key, Start = pos, Over = pos + p. poll}); // place the gift in the prize box range pos = Pos + p. poll;} int total = prizeList. sum (x => x. poll); // total number of gifts int speed = Math. max (total, peopleCount); // obtain the maximum number of gifts and the maximum number of parameters in the total number of pos = Prize. rand. next (speed); Area a = box. firstOrDefault (x => pos> = x. start & pos <x. over); // search for the index in the prize box. return a = null? String. empty:. key ;}}/* List <Prize> prizes = new List <Prize> (); prizes. add (new Prize () {Key = "computer", Poll = 1}); prizes. add (new Prize () {Key = "cabinet", Poll = 2}); prizes. add (new Prize () {Key = "Mouse", Poll = 3}); string lp1 = Prize. lunkyBox (prizes, 6); Console. writeLine (lp1); prizes. add (new Prize () {Key = "Thank you for your patronage", Poll = 5}); string lp2 = Prize. roulette (prizes); Console. writeLine (lp2 );*/

 

Related Article

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.