ตัวอย่างในบทความนี้แบ่งปันอัลกอริทึม Java Lottery สำหรับการอ้างอิงของคุณกับคุณ เนื้อหาเฉพาะมีดังนี้
1. การวิเคราะห์อัลกอริทึม
รางวัลแบ่งออกเป็นช่วงเวลาตามความน่าจะเป็น แต่ละช่วงเวลาแสดงถึงรางวัลจากนั้นทำการสุ่มตัวเลขและช่วงเวลาจะถูกตรวจสอบย้อนกลับซึ่งเป็นรางวัลที่วาด
2. รหัส
อัลกอริทึมหลัก
คณิตศาสตร์ระดับสาธารณะ {// กำลังขยายส่วนตัวคงที่ int mulriple = 1000000; Public Int Pay (รายการ <Prize> Pricings) {int lastscope = 0; // shuffle, ขัดขวางการรวบรวมคำสั่งซื้อรางวัล shuffle (รางวัล); แผนที่ <จำนวนเต็ม, int [] >> presecopes = new hashmap <จำนวนเต็ม, int []> (); แผนที่ <จำนวนเต็ม, จำนวนเต็ม> pricingquantity = new hashmap <จำนวนเต็ม, จำนวนเต็ม> (); สำหรับ (ราคารางวัล: รางวัล) {int priceid = price.getPrizeId (); // ช่วงเวลาพาร์ติชันกระแส int currentscope = lastscope + price.getProbability (). คูณ (ใหม่ bigdecimal (mulriple)). intvalue (); presecopes.put (prizeid, int ใหม่ [] {lastscope + 1, currentscope}); priceQuantity.put (prizeid, price.getQuantity ()); lastscope = currentscope; } // รับหมายเลขสุ่มระหว่าง 1-1000000 int luckynumber = new random (). nextint (mulriple); int luckyPrizeId = 0; // ค้นหาช่วงเวลาที่หมายเลขสุ่มอยู่ถ้า ((null! = ราคา) &&! สำหรับ (map.entry <จำนวนเต็ม, int []> m: รายการรายการ) {int key = m.getKey (); if (luckynumber> = m.getValue () [0] && luckynumber <= m.getValue () [1] && pricequantity.get (คีย์)> 0) {luckyPrizeId = key; หยุดพัก; }}} if (luckyPrizeId> 0) {// ลดคลังรางวัลหนึ่ง} return luckyPrizeId; - ถั่วรางวัล
รางวัลระดับสาธารณะ { / *** ฉลากที่ไม่ซ้ำกันสำหรับรางวัล* / Private Integer PrizeId; / *** ความน่าจะเป็นที่จะชนะ*/ ความน่าจะเป็นส่วนตัวใหญ่ / *** จำนวนรางวัล*/ ปริมาณจำนวนเต็มส่วนตัว; Public Integer getPrizeId () {return prizeId; } โมฆะสาธารณะ setPrizeId (จำนวนเต็ม prizeId) {this.prizeId = prizeId; } สาธารณะ bigDecimal getProbability () {ความน่าจะเป็นคืน; } โมฆะสาธารณะ setProbability (ความน่าจะเป็น bigdecimal) {this.probability = ความน่าจะเป็น; } Public Integer GetQuantity () {ปริมาณคืน; } โมฆะสาธารณะ setquantity (ปริมาณจำนวนเต็ม) {this.quantity = ปริมาณ; - 3. ทดสอบ
ความน่าจะเป็น Prize1: 5%
Prize2 ความน่าจะเป็น: 10%
Prize3 ความน่าจะเป็น: 15%
ความน่าจะเป็น Prize4: 20%
ความน่าจะเป็นรางวัล 5: 50%
การทดสอบระดับสาธารณะ {โมฆะคงที่สาธารณะหลัก (สตริง [] args) {รายการ <crize> pricings = new ArrayList <Prize> (); รางวัลราคา 1 = รางวัลใหม่ (); ราคา 1.SetPrizeId (1); Price1.SetProbability (ใหม่ BigDecimal (0.05)); ราคา 1.SetQuantity (1); ราคา ADD (รางวัล 1); Prize Price2 = รางวัลใหม่ (); price2.setPrizeid (2); Price2.SetProbability (ใหม่ BigDecimal (0.10)); price2.setQuantity (10); ราคา ADD (รางวัล 2); รางวัลราคา 3 = ใหม่รางวัล (); price3.setPrizeid (3); price3.setProbability (ใหม่ bigdecimal (0.15)); price3.setquantity (20); ราคา ADD (รางวัล 3); Prize Price4 = รางวัลใหม่ (); price4.setPrizeId (4); Price4.SetProbability (ใหม่ BigDecimal (0.20)); Price4.SetQuantity (50); ราคา ADD (รางวัล 4); Prize Price5 = รางวัลใหม่ (); price5.setPrizeid (5); price5.setProbability (ใหม่ bigdecimal (0.50)); price5.setquantity (200); ราคา ADD (รางวัล 5); int price1getTimes = 0; int price2getTimes = 0; int price3getTimes = 0; int price4getTimes = 0; int price5getTimes = 0; int price5getTimes = 0; เลขคณิตเลขคณิต = เลขคณิตใหม่ (); int times = 1,000; สำหรับ (int i = 0; i <times; i ++) {int priceid = arithmetic.pay (รางวัล); สวิตช์ (prizeId) {กรณีที่ 1: price1getTimes ++; หยุดพัก; กรณีที่ 2: Price2GetTimes ++; หยุดพัก; กรณีที่ 3: Price3getTimes ++; หยุดพัก; กรณีที่ 4: Price4getTimes ++; หยุดพัก; กรณีที่ 5: Price5GetTimes ++; หยุดพัก; }} system.out.println ("จำนวนลอตเตอรี" + ครั้ง); System.out.println ("Prize1 Wins" + Prize1getTimes); System.out.println ("Prize2 Wins" + Prize2getTimes); System.out.println ("Prize3 Wins" + Prize3getTimes); System.out.println ("Prize4 Wins" + Prize4getTimes); System.out.println ("Prize5 Wins" + Prize5GetTimes); - ผลลัพธ์:
ผ่านการสกัด 1,000 ครั้งเราจะเห็นว่าความแม่นยำของอัลกอริทึมยังคงสูงมาก
ข้างต้นเป็นเนื้อหาทั้งหมดของบทความนี้ ฉันหวังว่ามันจะเป็นประโยชน์ต่อการเรียนรู้ของทุกคนและฉันหวังว่าทุกคนจะสนับสนุน wulin.com มากขึ้น