上周日路过彩票店,买了注彩票(随机打的),试试手气看能不能中奖顺便为了福利做贡献

嗯,结果很现实,没中~~~ 当然是在我的预料之中

心想着买了这么多次,哪怕中个蓝色球呢,不止于这么惨吧,干脆自己写个程序分析下一等奖的中奖究竟有多难

说干就干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() {List<Integer> redList = new ArrayList<Integer>();for (int i=1; i<=33; i++) {redList.add(i);}List<Integer> blueList = new ArrayList<Integer>();for (int i=1; i<=16; i++) {blueList.add(i);}Random rand = new Random();List<Integer> tempList = new ArrayList<Integer>();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;//线程数List<Integer> digits = digits(cpNum,countDownLatchNum);List<Integer> list = new CopyOnWriteArrayList<Integer>();//存放返回结果CountDownLatch countDownLatch = new CountDownLatch(countDownLatchNum);ExecutorService executorService = Executors.newFixedThreadPool(countDownLatchNum);for (int i = 0; i < countDownLatchNum; i++) {Runnable runnable = new Runnable(){@Overridepublic 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<Integer> digits(int cpNum, int countDownLatchNum) {List<Integer> digitNums = new CopyOnWriteArrayList<Integer>();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<Integer> redList = new ArrayList<Integer>();for (int i=1; i<=33; i++) {redList.add(i);}List<Integer> blueList = new ArrayList<Integer>();for (int i=1; i<=16; i++) {blueList.add(i);}Random rand = new Random();List<Integer> tempList = new ArrayList<Integer>();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;//线程数List<Integer> digits = digits(cpNum,countDownLatchNum);List<Integer> list = new CopyOnWriteArrayList<Integer>();//存放返回结果CountDownLatch countDownLatch = new CountDownLatch(countDownLatchNum);ExecutorService executorService = Executors.newFixedThreadPool(countDownLatchNum);for (int i = 0; i < countDownLatchNum; i++) {Runnable runnable = new Runnable(){@Overridepublic 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<Integer> digits(int cpNum, int countDownLatchNum) {List<Integer> digitNums = new CopyOnWriteArrayList<Integer>();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<Integer> redList = new ArrayList<Integer>();for (int i=1; i<=35; i++) {redList.add(i);}List<Integer> blueList = new ArrayList<Integer>();for (int i=1; i<=12; i++) {blueList.add(i);}Random rand = new Random();List<Integer> tempRedList = new ArrayList<Integer>();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<Integer> tempBlueList = new ArrayList<Integer>();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();}}

看到这个结果,我就放心了,数量不少,如何彰显出天选之子之幸运~只可惜了我那两块钱,还不如直接捐给支付宝公益呐

利用Java程序统计彩票双色球中一等奖究竟有多难相关推荐

  1. 每个java程序都至少有一个线程给主线程,java程序在主线程中判断各个子线程状态的操作,该如何解决...

    java程序在主线程中判断各个子线程状态的操作 每个子线程在队列为空时会wait等待其他线程添加新url到队列,到最后所有子线程都取不到url时也会都wait住,要在主线程中判断如果所有的子线程都是w ...

  2. Java程序向MySql数据库中插入的中文数据变成了问号

    找到mysql的安装目录,修改my.ini文件 (1)如何找到my.ini文件 如果my.ini文件不在MySQL的安装目录下,可能放在隐藏目录,要先去找到ProgramData,(这里要先打开显示隐 ...

  3. java程序阅读题6,阅读以下说明和java程序,填补代码中的空缺(1)~(6),将解答填入答题纸的对 - 信管网...

    阅读以下说明和Java程序,填补代码中的空缺(1)-(6),将解答填入答题纸的对应栏内. [说明] 很多依托扑克牌进行的游戏都要先洗牌.下面的Java代码运行时先生成一副扑克牌,洗牌后再按顺序打印每张 ...

  4. Java程序员在面试中不通过的五个原因

    如今正是毕业生找工作的高峰期,那么在面试Java程序员的过程中会出现什么问题呢?有哪些问题是初入职场的Java程序员在面试中最容易犯的呢?下面,我总结了Java程序员在面试中不通过的五个原因,作为大家 ...

  5. 39岁java程序员,待业中,对前途很迷茫,每天都很焦虑,不知道该怎么办?

    39岁java程序员,待业中,对前途很迷茫,每天都很焦虑,不知道该怎么办?第一次看到这个问题,觉得挺无奈的.39岁这个年纪,工作应该也有10多年,对于程序员岗位,如果平时没有放弃学习,相信专业技术能力 ...

  6. 利用JAVA程序批量导入csv数据到MySQL数据库

    正在学习利用R进行统计学相关知识的实验,实验数据计划采用北京市环境监测数据,此数据可以在这个网址"https://quotsoft.net/air/"中下载,目前可提供2013年1 ...

  7. java基础—统计一个字符串中各个字符出现的次数

    统计一个字符串中各个字符出现的次数 import java.util.Iterator; import java.util.Set; import java.util.TreeMap;public c ...

  8. 解决Java程序在MOTO E680i中声音文件播放

    这几天一直在用MOTO SDK来开发一款用于Moto E680i的JAVA游戏,利用BLOG发表一些心得: MOTO系列手机中JAVA程序播放一个声音文件比较简单,但是用于交互式音效时就有问题了. 根 ...

  9. 完美解决Java程序在 MOTO E680i 中声音文件播放

    这几天一直在用MOTO SDK来开发一款用于Moto E680i的JAVA游戏,利用BLOG发表一些心得: MOTO系列手机中JAVA程序播放一个声音文件比较简单,但是用于交互式音效时就有问题了. 根 ...

  10. 完美解决Java程序在 MOTO E680i 中声音文件播放(转载)

    这几天一直在用MOTO SDK来开发一款用于Moto E680i的JAVA游戏,利用BLOG发表一些心得: MOTO系列手机中JAVA程序播放一个声音文件比较简单,但是用于交互式音效时就有问题了. 根 ...

最新文章

  1. 十一届蓝桥杯java组-蓝肽子序列-动态规划
  2. 有哪些专业学python_学习python有哪些好书和学习方法?
  3. c#自动向网页Post信息并提取返回的信息
  4. img disabled可以用什么替代_本特:马内不可替代,菲米是粘合剂,萨拉赫可以用姆巴佩桑乔替代...
  5. cmd mysql 报错_客户端cmd打开mysql,执行插入中文报错或插入中文乱码解决方案
  6. 在 C# 中生成代码的四种方式——包括.NET 5中的Source Generators
  7. python中try语句_[转]python 里面 try语句
  8. python学爬虫还是人工_本人初二生,学习Python爬虫,要不要继续深入学习啊?
  9. 教你如何用vbs实现微信自动发送消息功能
  10. 阿里云P10技术专家褚霸:我是一个程序员
  11. 数字滤波器的简单使用
  12. 乐吾乐le5le-Topology为智慧水务可视化赋能(一)
  13. 布谷鸟沙盒分析静态文件_布谷鸟cuckoo
  14. 【制作】基于金沙滩51单片机的电子跑表
  15. 计算机网络 第七章 网络安全
  16. 小学教师计算机校本培训材料,教师业务学习材料及校本培训材料
  17. 为什么新网站上线一个月都没有收录?
  18. install diagnostic_updater
  19. 《Android开发艺术探索》之Activity的生命周期和启动模式(一)
  20. 微信小程序设置底部tab选项卡

热门文章

  1. Pr入门系列之二:导入与管理素材
  2. 新浪博客服务器是不是在维护,近日新浪博客发布文章不能正常显示是为什么?...
  3. 如何修改PDF文件的标题
  4. 如何使用PS进行P图
  5. 一键清理系统垃圾文件
  6. android音乐视频播放器,android音乐视频播放器.doc
  7. 国际贸易通用邮箱,实时邮件配送
  8. 奥斯汀计算机专业排名,德克萨斯大学奥斯汀分校
  9. linux wrf 系统_linux wrf 系统
  10. 19个免费的UI界面设计工具及资源