Java Lottery snapping algorithm _java

Source: Internet
Author: User
Tags static class

This example for you to share the Java Lottery snapping algorithm for everyone to refer to, the specific contents are as follows

Application Scenarios

Single-piece trophy snapping (limited)
Multiple prizes are awarded according to Probability (limited, limited)

Code implementation

Table structure:

--Lottery Set
CREATE TABLE Award_info
(ID number (one) not     null,
 act_id number (one   ),  --Activity ID
 NUM    Number (one)--total number of  prizes (0 is unlimited)
 REST number    (one)-  -Prize margin
 odds number    (one) default 0,  --Jackpot Probability
 start_date date,     --Start date (nullable) end_date date  ,     --End Date (nullable) product_id number
 ( One)  --Trophy ID
 State number   (5) Default 0,  --Status 0-valid 1-fail
 info_type number (5) default 0   --0 -Normal 
);
ALTER TABLE Award_info
 add constraint Pk_award_info primary key (ID);

--Jackpot Record
CREATE TABLE Award_log
(ID number (one     ),  
 act_id number (one   ),  --Activity ID
 get_time  Date,  --Jackpot Time
 product_id number (one)-  -Trophy ID
 num number    (one) default 1, -  -Jackpot number   person varchar2,--winner (
 info_id)-  -lottery set ID
 state Number   (5)  --state 0-Valid 1-invalid
);
ALTER TABLE Award_log
 add constraint Pk_award_log primary key (ID);

Code:

  public static class awardresult{public int ret; Returns the result public int logid; Award_log ID}/** * lottery algorithm * @param actid lottery Activity ID * @param person Lottery * @param productId prize ID-1 is the activity ID  There are prizes * @param Excludeid excluding prizes ID-1 is not excluded, and ProductID can not be >0 * @param checkdate whether the check time * @return-1 no lottery data;-2 prizes have been drawn out; -3 other errors; >=0 winning ProductID; -4 Exclude ID * @throws Exception/public static awardresult getawardfull (int actid, String person, int productId, int 

    [] Excludeids, Boolean checkdate) throws sqlexception{awardresult result = new Awardresult ();
    Connection conn = Jdbc.getconnection ();
    Conn.setautocommit (FALSE);
      try{list<map<string,object>> rows;
      String SQL;
      String checkdatestr = ""; String basesql = "Select T.id, t.product_id, T.num, T.rest, T.odds, t.info_type from Award_info t where t.act_id=?"
      and t.state=0 "; if (checkdate) {checkdatestr = "and T.start_date <= sysdate and T.end_date >= Sysdate ";
        } if (ProductId > 0) {//snapping up sql = Basesql + "and t.product_id=?" + Checkdatestr + "for update";
      rows = jdbc.getrows (sql, New Object[]{actid, productId}, conn);
        }else{//activities all items Draw sql = Basesql + checkdatestr + "for update";
      rows = jdbc.getrows (sql, New Object[]{actid}, conn);  } if (Rows.isempty ()) {//No lottery data log.info ("No Raffle Data actid={} person={} productid={} excludeids={}".
        Actid, person, productId, Excludeids, checkdate);
        Conn.commit ();
        Result.ret =-1;
      return result;
      int infoid =-1;
      int getproductid =-1;
      int num =-1;
      int rest =-1;
        if (rows.size () = = 1) {//Snapping num = ((number) rows.get (0). Get ("num")). Intvalue ();
        Rest = ((number) rows.get (0). Get ("REST")). Intvalue ();
        Infoid = ((number) rows.get (0). Get ("ID"). Intvalue ();
      GetProductID = ((number) rows.get (0). Get ("product_id"). Intvalue ();
   }else{//Lottery     int[][] temp = new Int[rows.size ()][3];
        int sum =-1;
        int i = 0;
          for (int k = 0; k < rows.size (); k++) {//Set prize pool int odds = ((BigDecimal) Rows.get (k). Get ("odds")). Intvalue ();
          sum++; Temp[i][0] = sum;
          Start value sum = SUM + odds; TEMP[I][1] = sum;  End value temp[i][2] = k;
        Rows index i++;

        }//Draw Random Random = new Random ();
        int r = random.nextint (sum + 1);
        int j = 0;
            for (int k = 0; k < i; k++) {if (R >= temp[k][0] && r <= temp[k][1]) {j = k;
          Break
        } infoid = ((BigDecimal) Rows.get (temp[j][2)). Get ("ID"). Intvalue ();
        GetProductID = ((BigDecimal) Rows.get (temp[j][2)). Get ("product_id"). Intvalue ();
        num = ((number) Rows.get (temp[j][2]). Get ("NUM"). Intvalue ();
      Rest = ((number) Rows.get (temp[j][2]). Get ("REST"). Intvalue (); //Decide whether to exclude ID if(Arrayutils.contains (Excludeids, GetProductID)) {Log.info ("is exclusion ID actid={} person={} productid={} excludeids={} checkdate={}", Actid, Person, productId, Excludeid
        s, checkdate);
        Conn.commit ();
        Result.ret =-4;
      return result; ///stock less if (num > 0 && rest <= 0) {log.info ("prize emptied actid={} person={} productid={} excl"
        udeids={} checkdate={} ", Actid, Person, productId, Excludeids, checkdate);
        JDBC.COMMIT (conn);
        Result.ret =-2;
      return result;
        //Update prize record if (num > 0) {//non-unlimited sql = "Update award_info set rest = rest-1 where id =?";
      Jdbc.update (SQL, New object[]{infoid}, conn);
      //Record winning list Awardlog log = new Awardlog ();
      Log.setactid (Actid);
      Log.setnum (1);
      Log.setperson (person);
      Log.setproductid (GetProductID);
      Log.setinfoid (infoid);
      Number Logid = Log.save (conn); if (Logid = = null) {throw new Sqlexception ("Save Award_log Error");

      } Result.logid = Logid.intvalue ();
      Conn.commit ();
      Result.ret = GetProductID;

    return result;
      }catch (SQLException e) {log.error ("Getaward error", E);
    Conn.rollback ();
    }finally{Jdbc.close (conn);
    } Result.ret =-3;
  return result;

 }

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.