十行代码写个彩票随机生成器(双色球+大乐透)

1,582次阅读
没有评论

规则

双色球(红球33选6,蓝球16选1)
大乐透(红球35选5,蓝球12选2)

生成随机数

  1. 生成的数要不重复且在规则范围内
  2. for循环+do while+boolean去重
    (当然这样的效率很低,可是暂且够用就行了,如果想知道优化版怎么做的话,请移步此博客:)

代码

客户端代码

public class DoubleChomper {
    //双色球红球范围(所有期望值+1)
   public static int redRange = 34;
    //双色球红球个数
    public static int redFrequency = 6;
    //双色球蓝球范围
    public static int blueRange = 17;
    //双色球蓝球个数
    public static int blueFrequency = 1;
    //大乐透红球范围
    public static int leTouRedRange =36;
    //大乐透红球个数
    public static int leTouRedFrequency = 5;
    //大乐透蓝球范围
    public static int leTouBlueRange = 13;
    //大乐透蓝球范围
    public static int leTouBlueFrequency = 2;

    public static void main(String[] args) {
        getResult(10,10);
    }
复制代码

核心代码

	/**
     * 彩票选球
     * @param range 选球范围
     * @param number 选球个数
     * @return
     */
    private static List getTheBall(int range, int number){
        //定义接收数组
        List red_list =new ArrayList(6);
        Random rand = new Random();
        boolean[] bool = new boolean[range];
        int randInt = 0;
        //循环生成随机数去重后添加到接收数组中
        for(int i = 0; i < number ; i++) {
            do {
                randInt = rand.nextInt(range-1)+1;
            } while (bool[randInt]);
            bool[randInt] = true;
            red_list.add(randInt);
        }
        //结果集排序
        red_list.sort(Comparator.naturalOrder());
        return red_list;
    }
复制代码

打印

	/**
     * 循环打印结果
     * @param doubleColorNum 双色球生成号码注数
     * @param leTouNum 大乐透生成号码注数
     */
    public static void getResult(int doubleColorNum , int leTouNum ){
        System.out.println("双色球机选结果");
        for (int i = 0; i <doubleColorNum ; i++) {
            List red = getTheBall(redRange,redFrequency);
            List blue = getTheBall(blueRange,blueFrequency);
            System.out.println("红球: "+red+";蓝球:  "+blue);
        }
        System.out.println("大乐透机选结果");
        for (int i = 0; i <leTouNum ; i++) {
            List red = getTheBall(leTouRedRange,leTouRedFrequency);
            List blue = getTheBall(leTouBlueRange,leTouBlueFrequency);
            System.out.println("红球:"+red+";   蓝球:" + blue);
        }
    }

正文完
可以使用微信扫码关注公众号(ID:xzluomor)
post-qrcode
 0
评论(没有评论)

文心AIGC

2023 年 4 月
 12
3456789
10111213141516
17181920212223
24252627282930
文心AIGC
文心AIGC
人工智能ChatGPT,AIGC指利用人工智能技术来生成内容,其中包括文字、语音、代码、图像、视频、机器人动作等等。被认为是继PGC、UGC之后的新型内容创作方式。AIGC作为元宇宙的新方向,近几年迭代速度呈现指数级爆发,谷歌、Meta、百度等平台型巨头持续布局
文章搜索
热门文章
潞晨尤洋:日常办公没必要上私有模型,这三类企业才需要 | MEET2026

潞晨尤洋:日常办公没必要上私有模型,这三类企业才需要 | MEET2026

潞晨尤洋:日常办公没必要上私有模型,这三类企业才需要 | MEET2026 Jay 2025-12-22 09...
“昆山杯”第二十七届清华大学创业大赛决赛举行

“昆山杯”第二十七届清华大学创业大赛决赛举行

“昆山杯”第二十七届清华大学创业大赛决赛举行 一水 2025-12-22 17:04:24 来源:量子位 本届...
MiniMax海螺视频团队首次开源:Tokenizer也具备明确的Scaling Law

MiniMax海螺视频团队首次开源:Tokenizer也具备明确的Scaling Law

MiniMax海螺视频团队首次开源:Tokenizer也具备明确的Scaling Law 一水 2025-12...
真正面向大模型的AI Infra,必须同时懂模型、系统、产业|商汤大装置宣善明@MEET2026

真正面向大模型的AI Infra,必须同时懂模型、系统、产业|商汤大装置宣善明@MEET2026

真正面向大模型的AI Infra,必须同时懂模型、系统、产业|商汤大装置宣善明@MEET2026 量子位的朋友...
最新评论
ufabet ufabet มีเกมให้เลือกเล่นมากมาย: เกมเดิมพันหลากหลาย ครบทุกค่ายดัง
tornado crypto mixer tornado crypto mixer Discover the power of privacy with TornadoCash! Learn how this decentralized mixer ensures your transactions remain confidential.
ดูบอลสด ดูบอลสด Very well presented. Every quote was awesome and thanks for sharing the content. Keep sharing and keep motivating others.
ดูบอลสด ดูบอลสด Pretty! This has been a really wonderful post. Many thanks for providing these details.
ดูบอลสด ดูบอลสด Pretty! This has been a really wonderful post. Many thanks for providing these details.
ดูบอลสด ดูบอลสด Hi there to all, for the reason that I am genuinely keen of reading this website’s post to be updated on a regular basis. It carries pleasant stuff.
Obrazy Sztuka Nowoczesna Obrazy Sztuka Nowoczesna Thank you for this wonderful contribution to the topic. Your ability to explain complex ideas simply is admirable.
ufabet ufabet Hi there to all, for the reason that I am genuinely keen of reading this website’s post to be updated on a regular basis. It carries pleasant stuff.
ufabet ufabet You’re so awesome! I don’t believe I have read a single thing like that before. So great to find someone with some original thoughts on this topic. Really.. thank you for starting this up. This website is something that is needed on the internet, someone with a little originality!
ufabet ufabet Very well presented. Every quote was awesome and thanks for sharing the content. Keep sharing and keep motivating others.
热评文章
小米大模型“杀”进第一梯队:代码能力开源第一,智商情商全在线

小米大模型“杀”进第一梯队:代码能力开源第一,智商情商全在线

小米大模型“杀”进第一梯队:代码能力开源第一,智商情商全在线 克雷西 2025-12-18 08:57:11 ...
ISC.AI 2025创新百强颁奖典礼落幕,首发智能体专家驱动产业升级

ISC.AI 2025创新百强颁奖典礼落幕,首发智能体专家驱动产业升级

ISC.AI 2025创新百强颁奖典礼落幕,首发智能体专家驱动产业升级 量子位的朋友们 2025-12-18 ...
具身智能的数据难题,终于有了可规模化的解法

具身智能的数据难题,终于有了可规模化的解法

具身智能的数据难题,终于有了可规模化的解法 思邈 2025-12-18 14:20:44 来源:量子位 成立4...
医生版ChatGPT,估值120亿美元

医生版ChatGPT,估值120亿美元

医生版ChatGPT,估值120亿美元 Jay 2025-12-18 13:45:12 来源:量子位 Jay ...
国产AI芯片看两个指标:模型覆盖+集群规模能力 | 百度智能云王雁鹏@MEET2026

国产AI芯片看两个指标:模型覆盖+集群规模能力 | 百度智能云王雁鹏@MEET2026

国产AI芯片看两个指标:模型覆盖+集群规模能力 | 百度智能云王雁鹏@MEET2026 西风 2025-12-...