Java Edition based on the Weighted lottery algorithm

Source: Internet
Author: User

The algorithm used to extract weights is widely used, and the lottery is one of the main uses. Just these days are also in the development of the lottery module, the entire lottery module involves three places, the background is to add prizes (at the same time to set weights and quantity), the front desk based on the background configuration to generate a lottery queue and according to the instructions to start the lottery, the last part of the background statistics and set up the status of logistics. This paper mainly focuses on the foreground lottery algorithm to introduce how to set the probability of each prize being pumped according to the weight.

The core of the lottery algorithm is to set the probability of a random number based on the weight, where I encapsulate it as a random class that generates random numbers, with the following code:

/** * JAVA Returns a random number, based on probability, ratio * */public class Mathrandom {private static Log logger = Logfactory.getlog (mathrandom.class);/* * * Math.random () produces a double random number, judging the probability of each prize appearing * * @return int * */public int percentagerandom (list<rewardprize>  Prizes) {DecimalFormat df = new DecimalFormat ("##### #0.00"); int random = -2;try{double Sumweight = 0;//calculates the total weight for (rewardprize rp_1:prizes) {sumweight + = Rp_1.getprize_weight ();} Double randomnumber;randomnumber = Math.random (); System.out.println ("Randomnumber is:" + randomnumber);d ouble d1 = 0;double D2 = 0;for (int i=0;i<prizes.size (); i++) {D2 + = Double.parsedouble (string.valueof (Prizes.get (i). Getprize_weight ()))/sumweight;if (i==0) {d1 = 0;} Else{d1 +=double.parsedouble (string.valueof (Prizes.get (i-1). Getprize_weight ()))/sumweight;} if (randomnumber >= d1 && randomnumber <= d2) {random = i; System.out.println ("D1 is:" + D1); System.out.println ("D2 is:" + D2); catch (Exception e) {System.out.println (E.getmessage ()); Logger.error ("Error generating lottery random number,Cause of Error: "+ e.getmessage ()); random =-1;} return random;} /** * Test Main program * * @param agrs */public static void Main (string[] agrs) {int i = 0; Mathrandom a = new Mathrandom (); List<rewardprize> prizes = new ArrayList (); for (int m=0;m<100;m++) {rewardprize RP = new Rewardprize (); Rp.setprize_amount (10);//Each prize quantity is set 10 rp.setprize_weight (1);//The weight of each prize is set to 1, That is, each prize is drawn to the same probability (you can set the weight according to the situation) Prizes.add (RP);} for (i = 0; I <=; i++)//Print 100 test probability accuracy {System.out.println (A.percentagerandom (Prizes));}}}


A brief introduction to the above meaning of the code, first calculate the total weight of the prize to be selected, so that the purpose is to set the prize weight arbitrarily, no longer consider the sum of the weight is equal to 100. The random rule is to first generate a random number randomnumber (generated random digits from 0 to 1 of the left open closing interval), and then calculate each of the current prizes before the prize (excluding the current prize) probability and D1 and the current prize (including the current prize) the probability and D2 of all prizes, It then determines whether the generated random number randomnumber is already in between D1 and D2, and if it is within that interval, the current prize will be drawn.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Java Edition based on the Weighted lottery algorithm

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.