利用Java程序统计彩票双色球中一等奖究竟有多难
发布日期:2021-06-30 16:22:57
浏览次数:3
分类:技术文章
本文共 10115 字,大约阅读时间需要 33 分钟。
上周日路过彩票店,买了注彩票(随机打的),试试手气看能不能中奖顺便为了福利做贡献
嗯,结果很现实,没中~~~ 当然是在我的预料之中
心想着买了这么多次,哪怕中个蓝色球呢,不止于这么惨吧,干脆自己写个程序分析下一等奖的中奖究竟有多难
说干就干10分钟后程序写出来了 ,但执行的时候,醉了0.0 TM实在是等不及了
package com.data.cp;import java.util.ArrayList;import java.util.Collections;import java.util.List;import java.util.Random;public class CpTest { public static void main(String[] args) { // 2020-105期 本期销售 399513718 两元一注则共199756859注彩票 int cpNum = 199756859; String cpOneStr = "06-14-19-20-22-24-01"; //getCpStr(); //随机一注 Long startTime = System.currentTimeMillis(); //for循环 int cpOneNum = getCpOneNumFunc1(cpOneStr,cpNum); System.out.println("中奖号码" + cpOneStr + " , 模拟"+ cpNum + "注彩票中预计一等奖数量 :" + cpOneNum); Long endTime = System.currentTimeMillis(); System.out.println("用时:" + (endTime - startTime + "毫秒")); } private static int getCpOneNumFunc1(String cpOneStr, int cpNum) { int cpOneNum = 0; String cpTempStr = ""; for (int i=1; i<=cpNum; i++) { cpTempStr = getCpStr(); if(cpTempStr.equals(cpOneStr)) { cpOneNum++; } } return cpOneNum; } private static String getCpStr() { ListredList = new ArrayList (); for (int i=1; i<=33; i++) { redList.add(i); } List blueList = new ArrayList (); for (int i=1; i<=16; i++) { blueList.add(i); } Random rand = new Random(); List tempList = new ArrayList (); for (int i=1; i<=6; i++) { int randRed = rand.nextInt(redList.size()); tempList.add(redList.get(randRed)); redList.remove(randRed); } Collections.sort(tempList); int randBlue = rand.nextInt(blueList.size()); tempList.add(blueList.get(randBlue)); StringBuffer sb = new StringBuffer(); int spl = 0; for (Integer integer : tempList) { String numStr = integer.toString(); if(integer < 10) { numStr = "0" + numStr; } if(spl < 6) { numStr += "-"; } sb.append(numStr); spl++; } //System.out.println(sb.toString()); return sb.toString(); } }
10分钟没执行完 ,我是不想等了...程序执行的时间超出了我写程序的时间 果断要优化,于是加入了多线程
package com.data.cp;import java.util.ArrayList;import java.util.Collections;import java.util.List;import java.util.Random;import java.util.concurrent.CopyOnWriteArrayList;import java.util.concurrent.CountDownLatch;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;public class CpTest { public static void main(String[] args) { // 2020-105期 本期销售 399513718 两元一注则共199756859注彩票 int cpNum = 199756859; String cpOneStr = "06-14-19-20-22-24-01"; //getCpStr(); //随机一注 Long startTime = System.currentTimeMillis(); //for循环 //int cpOneNum = getCpOneNumFunc1(cpOneStr,cpNum);// 10分钟没执行完 ,我是不想等了... //多线程处理 int cpOneNum = getCpOneNumFunc2(cpOneStr,cpNum);//多线程处理 60s左右 System.out.println("中奖号码" + cpOneStr + " , 模拟"+ cpNum + "注彩票中预计一等奖数量 :" + cpOneNum); Long endTime = System.currentTimeMillis(); System.out.println("用时:" + (endTime - startTime + "毫秒")); } private static int getCpOneNumFunc1(String cpOneStr, int cpNum) { int cpOneNum = 0; String cpTempStr = ""; for (int i=1; i<=cpNum; i++) { cpTempStr = getCpStr(); if(cpTempStr.equals(cpOneStr)) { cpOneNum++; } } return cpOneNum; } private static int getCpOneNumFunc2(String cpOneStr, int cpNum) { int cpOneNum = 0; int countDownLatchNum = 1000;//线程数 Listdigits = digits(cpNum,countDownLatchNum); List list = new CopyOnWriteArrayList ();//存放返回结果 CountDownLatch countDownLatch = new CountDownLatch(countDownLatchNum); ExecutorService executorService = Executors.newFixedThreadPool(countDownLatchNum); for (int i = 0; i < countDownLatchNum; i++) { Runnable runnable = new Runnable(){ @Override public void run() { try { String name = Thread.currentThread().getName(); String[] split = name.split("-"); Integer threadId = Integer.parseInt(split[split.length-1]) - 1; Integer taskNum = digits.get(threadId); System.out.println("线程name:" + name + " \t执行任务数量:" + taskNum); int cpOneNumRunnable = getCpOneNumFunc1(cpOneStr,taskNum); //System.out.println("线程name:" + name + " \t执行完毕结果:" + cpOneNumRunnable); list.add(cpOneNumRunnable); countDownLatch.countDown(); } catch (Exception e) { e.printStackTrace(); } } }; executorService.execute(runnable); } try { countDownLatch.await(); } catch (InterruptedException e) { e.printStackTrace(); } executorService.shutdown(); for (Integer result : list) { cpOneNum += result; } return cpOneNum; } private static List digits(int cpNum, int countDownLatchNum) { List digitNums = new CopyOnWriteArrayList (); if(cpNum % countDownLatchNum > 0) { countDownLatchNum = countDownLatchNum-1; } int spl = cpNum / countDownLatchNum; for (int i = 0; i < countDownLatchNum; i++) { digitNums.add(spl); } if(cpNum % countDownLatchNum > 0) { digitNums.add(cpNum % countDownLatchNum); } return digitNums; } private static String getCpStr() { List redList = new ArrayList (); for (int i=1; i<=33; i++) { redList.add(i); } List blueList = new ArrayList (); for (int i=1; i<=16; i++) { blueList.add(i); } Random rand = new Random(); List tempList = new ArrayList (); for (int i=1; i<=6; i++) { int randRed = rand.nextInt(redList.size()); tempList.add(redList.get(randRed)); redList.remove(randRed); } Collections.sort(tempList); int randBlue = rand.nextInt(blueList.size()); tempList.add(blueList.get(randBlue)); StringBuffer sb = new StringBuffer(); int spl = 0; for (Integer integer : tempList) { String numStr = integer.toString(); if(integer < 10) { numStr = "0" + numStr; } if(spl < 6) { numStr += "-"; } sb.append(numStr); spl++; } //System.out.println(sb.toString()); return sb.toString(); } }
效果貌似不错,不到1分钟就出结果了
嗯 多试几次
艾玛 福彩诚不欺我 一等奖数量确实概率小的可怜,我这还是全部随机的单注,想想有些人几倍的投,这个概率确实正常
再来算一下双色球有多少种排列组合
双色球的排列组合一共 17721088 种 ,大奖虽好、还是放弃幻想,拥抱现实吧~~
顺便再来分析下体彩大乐透,它的规则是这样的
第六条 超级大乐透基本投注是指从前区号码中任选5个号码,并从后区号码中任选2个号码的组合进行投注。其中,前区号码由01—35共35个号码组成,后区号码由01—12共12个号码组成。每注基本投注金额人民币2元。购买者在基本投注的基础上,可对购买的每注号码进行一次追加投注,每注追加投注金额人民币1元。
它的玩法与双色球有相似的地方都是7个号码,只不过是5红2蓝构成,同时红色是1-35,蓝色1-12乍一看感觉比双色球概率大一些,一算就明白了,他的排列组合数为21425712,竟然比双色球都多!写个程序分析下
package com.data.cp;import java.util.ArrayList;import java.util.Collections;import java.util.List;import java.util.Random;import java.util.concurrent.CopyOnWriteArrayList;import java.util.concurrent.CountDownLatch;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;public class CpBigLottoTest { public static void main(String[] args) { // 2020-107期 本期销售:¥282,777,346元 两元一注则共141388673注彩票 int cpNum = 141388673; String cpOneStr = "01-04-13-17-28-03-07"; //getCpStr(); //随机一注 Long startTime = System.currentTimeMillis(); //多线程处理 int cpOneNum = getCpOneNumFunc1(cpOneStr,cpNum);//多线程处理 60s左右 System.out.println("中奖号码" + cpOneStr + " , 模拟"+ cpNum + "注彩票中预计一等奖数量 :" + cpOneNum); Long endTime = System.currentTimeMillis(); System.out.println("用时:" + (endTime - startTime + "毫秒")); } private static int getCpOneNumFunc1(String cpOneStr, int cpNum) { int cpOneNum = 0; int countDownLatchNum = 1000;//线程数 Listdigits = digits(cpNum,countDownLatchNum); List list = new CopyOnWriteArrayList ();//存放返回结果 CountDownLatch countDownLatch = new CountDownLatch(countDownLatchNum); ExecutorService executorService = Executors.newFixedThreadPool(countDownLatchNum); for (int i = 0; i < countDownLatchNum; i++) { Runnable runnable = new Runnable(){ @Override public void run() { try { String name = Thread.currentThread().getName(); String[] split = name.split("-"); Integer threadId = Integer.parseInt(split[split.length-1]) - 1; Integer taskNum = digits.get(threadId); System.out.println("线程name:" + name + " \t执行任务数量:" + taskNum); int cpOneNumRunnable = getCpOneNumFunc(cpOneStr,taskNum); //System.out.println("线程name:" + name + " \t执行完毕结果:" + cpOneNumRunnable); list.add(cpOneNumRunnable); countDownLatch.countDown(); } catch (Exception e) { e.printStackTrace(); } } private int getCpOneNumFunc(String cpOneStr, int cpNum) { int cpOneNum = 0; String cpTempStr = ""; for (int i=1; i<=cpNum; i++) { cpTempStr = getCpStr(); if(cpTempStr.equals(cpOneStr)) { cpOneNum++; } } return cpOneNum; } }; executorService.execute(runnable); } try { countDownLatch.await(); } catch (InterruptedException e) { e.printStackTrace(); } executorService.shutdown(); for (Integer result : list) { cpOneNum += result; } return cpOneNum; } private static List digits(int cpNum, int countDownLatchNum) { List digitNums = new CopyOnWriteArrayList (); if(cpNum % countDownLatchNum > 0) { countDownLatchNum = countDownLatchNum-1; } int spl = cpNum / countDownLatchNum; for (int i = 0; i < countDownLatchNum; i++) { digitNums.add(spl); } if(cpNum % countDownLatchNum > 0) { digitNums.add(cpNum % countDownLatchNum); } return digitNums; } private static String getCpStr() { List redList = new ArrayList (); for (int i=1; i<=35; i++) { redList.add(i); } List blueList = new ArrayList (); for (int i=1; i<=12; i++) { blueList.add(i); } Random rand = new Random(); List tempRedList = new ArrayList (); for (int i=1; i<=5; i++) { int randRed = rand.nextInt(redList.size()); tempRedList.add(redList.get(randRed)); redList.remove(randRed); } Collections.sort(tempRedList); List tempBlueList = new ArrayList (); for (int i=1; i<=2; i++) { int randBlue = rand.nextInt(blueList.size()); tempBlueList.add(blueList.get(randBlue)); blueList.remove(randBlue); } Collections.sort(tempBlueList); for (Integer blueNum : tempBlueList) { tempRedList.add(blueNum); } StringBuffer sb = new StringBuffer(); int spl = 0; for (Integer num : tempRedList) { String numStr = num.toString(); if(num < 10) { numStr = "0" + numStr; } if(spl < 6) { numStr += "-"; } sb.append(numStr); spl++; } //System.out.println(sb.toString()); return sb.toString(); }}
看到这个结果,我就放心了,数量不少,如何彰显出天选之子之幸运~只可惜了我那两块钱,还不如直接捐给支付宝公益呐
转载地址:https://kevin.blog.csdn.net/article/details/109332276 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
初次前来,多多关照!
[***.217.46.12]2024年04月20日 15时31分22秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
lnmp部署
2019-04-30
nginx平滑升级
2019-04-30
location区段
2019-04-30
nginx访问控制、基于用户认证、https配置
2019-04-30
用zabbix监控nginx
2019-04-30
rewrite和if语句
2019-04-30
nginx实现负载均衡和动静分离
2019-04-30
SaltStack
2019-04-30
Packer 如何将 JSON 的配置升级为 HCL2
2019-04-30
Ubuntu 安装 NTP 服务
2019-04-30
NeoFetch - Linux 使用命令行查看系统信息
2019-04-30
Jenkins 控制台输出中的奇怪字符
2019-04-30
Linux添加系统调用
2019-04-30
ubuntu 18 CTF 环境搭建
2019-04-30
linux内存的寻址方式
2019-04-30
[off by null + tcache dup]lctf_easy_heap
2019-04-30
[pie+libc]national2021_pwny
2019-04-30
task_struct 结构分析
2019-04-30