java等概率的抽取一個四位數2
阿新 • • 發佈:2019-02-12
package com.chinatricon.spring;import java.util.ArrayList;import java.util.HashSet;import java.util.Iterator;import java.util.Set;/** * 要求:
* 1 每次取一個,共取4次,4次組成一個4位數,拼起來為Set中的數
* 2 每次取數,Set中的每個數都有相同的機會 */public class LuckyGetter {public Set set = new HashSet();public static void main(String[] args) {// 初始化SetLuckyGetter getter = new LuckyGetter();getter.initSet(getter.set);// 取數long start = System.currentTimeMillis();for (int i = 0; i < 2000; i++) {System.out.println(getter.getRandom());}long end = System.currentTimeMillis();System.out.println(end - start);}public int getRandom() {int num = 0;for (int i = 0; i < 4; i++) {num = getNext(num);}return num;}/** * 1 如果num為0,返回千位
* 2 如果num為1位,返回千位和百位,以此類推 */public int getNext(int num) {if (num < 0 || num > 9999) {return 0;}int nextNum = 0;// 請把您的程式碼填寫在這裡nextNum = test2(num);return nextNum;}public void initSet(Set set) {set.add(1235);set.add(1236);set.add(1245);set.add(1248);set.add(1366);set.add(1369);set.add(1382);set.add(1384);set.add(2235);set.add(2236);set.add(2245);set.add(2248);set.add(2366);set.add(2369);set.add(2382);set.add(2384);set.add(8514);}public int test(int num) {ArrayList list = new ArrayList();for (Iterator it = set.iterator(); it.hasNext();) {Integer number = it.next();if (number / 10000 == num) {list.add(number / 1000);} else if (number / 1000 == num) {list.add(number / 100);} else if (number / 100 == num) {list.add(number / 10);} else if (number / 10 == num) {list.add(number);}}return list.get((int) (Math.random() * list.size()));}public int test2(int num) {ArrayList list = new ArrayList();for (Iterator it = set.iterator(); it.hasNext();) {String number = String.valueOf(it.next());if (number.startsWith(String.valueOf(num))) {list.add(Integer.parseInt(number.substring(0, String.valueOf(num).length() + 1)));} else if (num == 0) {list.add(Integer.parseInt(number.substring(0, 1)));}}return list.get((int) (Math.random() * list.size()));}}
* 1 每次取一個,共取4次,4次組成一個4位數,拼起來為Set中的數
* 2 每次取數,Set中的每個數都有相同的機會 */public class LuckyGetter {public Set set = new HashSet();public static void main(String[] args) {// 初始化SetLuckyGetter getter = new LuckyGetter();getter.initSet(getter.set);// 取數long start = System.currentTimeMillis();for (int i = 0; i < 2000; i++) {System.out.println(getter.getRandom());}long end = System.currentTimeMillis();System.out.println(end - start);}public int getRandom() {int num = 0;for (int i = 0; i < 4; i++) {num = getNext(num);}return num;}/** * 1 如果num為0,返回千位
* 2 如果num為1位,返回千位和百位,以此類推 */public int getNext(int num) {if (num < 0 || num > 9999) {return 0;}int nextNum = 0;// 請把您的程式碼填寫在這裡nextNum = test2(num);return nextNum;}public void initSet(Set set) {set.add(1235);set.add(1236);set.add(1245);set.add(1248);set.add(1366);set.add(1369);set.add(1382);set.add(1384);set.add(2235);set.add(2236);set.add(2245);set.add(2248);set.add(2366);set.add(2369);set.add(2382);set.add(2384);set.add(8514);}public int test(int num) {ArrayList list = new ArrayList();for (Iterator it = set.iterator(); it.hasNext();) {Integer number = it.next();if (number / 10000 == num) {list.add(number / 1000);} else if (number / 1000 == num) {list.add(number / 100);} else if (number / 100 == num) {list.add(number / 10);} else if (number / 10 == num) {list.add(number);}}return list.get((int) (Math.random() * list.size()));}public int test2(int num) {ArrayList list = new ArrayList();for (Iterator it = set.iterator(); it.hasNext();) {String number = String.valueOf(it.next());if (number.startsWith(String.valueOf(num))) {list.add(Integer.parseInt(number.substring(0, String.valueOf(num).length() + 1)));} else if (num == 0) {list.add(Integer.parseInt(number.substring(0, 1)));}}return list.get((int) (Math.random() * list.size()));}}