This example shares the Java lottery rush algorithm for your reference. The specific content is as follows
Application scenarios
Single-piece prize purchase (limited time)
Multiple prizes won by chance (limited time, no limit)
Code implementation
Table structure:
--Raffle Settings create table AWARD_INFO( ID NUMBER(11) not null, ACT_ID NUMBER(11), --Activity ID NUM NUMBER(11), --Total prizes (0 is unlimited) REST NUMBER(11), --Price allowance ODDS NUMBER(11) default 0, --Price probability START_DATE DATE, --Start date (can be empty) END_DATE DATE, --End date (can be empty) PRODUCT_ID NUMBER(11), --Price ID STATE NUMBER(5) default 0, --Status 0-Valid 1-Vailable INFO_TYPE NUMBER(5) default 0 --0-Normal); alter table AWARD_INFO add constraint PK_AWARD_INFO primary key (ID); --Winning record create table AWARD_LOG( id number(11), act_id number(11), --Activity ID get_time date, --Winning time product_id number(11), --Price ID number(11) default 1, --Winning number person varchar2(50), --Winner info_id number(11), --Raffle Setting ID state number(5) --Status 0-valid 1-invalid);alter table AWARD_LOG add constraint PK_AWARD_LOG primary key (ID);
Code:
public static class AwardResult{ public int ret; //Return result public int logId; //AWARD_LOG id } /** * Luck draw algorithm* @param actId Luck draw activity ID * @param person Luck draw person* @param productId Prize ID -1 is all prizes under the activity ID* @param excludeId Exclude prize ID -1 is not excluded, and cannot be simultaneously with productId>0 * @param checkDate Whether to check the time* @return -1 There is no lottery data; -2 The prize has been drawn; -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){//Snap sql = baseSql + " and t.product_id=? " + checkDateStr + " for update"; rows = JDBC.getRows(sql, new Object[]{actId, productId}, conn); }else{//All items draws for activity sql = baseSql + checkDateStr + " for update"; rows = JDBC.getRows(sql, new Object[]{actId}, conn); } if(rows.isEmpty()){//No draw data log.info("No draw data actId={} person={} productId={} excludeIds={} checkDate={}", 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){//Snap 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{//Rull draw int[][] temp = new int[rows.size()][3]; int sum = -1; int i = 0; for(int k = 0; k < rows.size(); k++){//Set the 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++; } //Roll 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(); } //Judge whether to exclude id if(ArrayUtils.contains(excludeIds, getProductId)){ log.info(" is the exclude ID actId={} person={} productId={} excludeIds={} checkDate={}", actId, person, productId, excludeIds, checkDate); conn.commit(); result.ret = -4; return result; } //Insufficient stock if(num > 0 && rest <= 0){ log.info("The prize has been cleared actId={} person={} productId={} excludeIds={} checkDate={}", actId, person, productId, excludeIds, checkDate); JDBC.commit(conn); result.ret = -2; return result; } //Update the prize record if(num > 0){//Not unlimited sql = "update award_info set rest = rest - 1 where id = ?"; JDBC.update(sql, new Object[]{infoId}, conn); } //Record the list of winners 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 all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.