JAVA Two simple lottery algorithm

Source: Internet
Author: User

Not much to say, the code is not many, the algorithm is simple


Method One:

/** * Algorithm for winning numbers, the method will fill the last number to this position after each draw to a winning number, implementing the non-repeating extraction. * <p> * After many tests, this method at Big data speed is slightly better than {@link #booleans ()}, small data gaps can be ignored *  * @return */public int[] Deletelast () {if (prize <= 0) return new int[0];int[] Nums = new int[max];for (int i=0; i<max; i++) {nums[i] = i+1;} Int[] Prizes = new Int[prize];int ZJ = 0;for (int i=0; i<prize; i++) {ZJ = (int) (Math.random () * max);p rizes[i] = nums[z J];NUMS[ZJ] = Nums[--max];} return prizes;}

Method Two:

/** * Gets the algorithm of the winning number, method sets the value of the original array to be Boolean, the value is set to true in the extraction, and the implementation does not repeat the extraction. * <p> * After many tests, this method at Big data speed is slightly worse than {@link #deleteLast ()}, small data gaps can be ignored *  * @return */public int[] Booleans () {if (prize <= 0) return new int[0];boolean[] Nums = new boolean[max];for (int i=0; i<max; i++) {Nums[i] = false;} Int[] Prizes = new Int[prize];int Zjcount = 0;int ZJ = 0;while (Zjcount < prize) {ZJ = (int) (Math.random () * max); if (!nu MS[ZJ]) {NUMS[ZJ] = true;prizes[zjcount++] = ZJ;}} return prizes;}


Reference: http://bbs.csdn.net/topics/280080151

JAVA Two simple 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.