• 双色球机选算法java实现


    双色球机选算法java实现

    一、代码

    package com.hdwang;
    
    import java.util.Random;
    
    /**
     * Created by admin on 2017/1/10.
     * 双色球机选实现
     */
    public class Ssq {
    
        public static void main(String[] args) {
    
            int arrayNums = 5; //机选5组
            for(int i=0;i<arrayNums;i++){
                String hm = getYZHM();
                System.out.println(hm);
            }
        }
    
        /**
         * 产生一组双色球选号
         * @return 一注号码
         */
        private static String getYZHM(){
           //33选6
            int[] exist = new int[6]; //默认全0
            for(int i=0;i<6;i++){
                int ball = getABall(33,exist);
                exist[i] = ball; //暂存已选的球
            }
            sort(exist); //排序
            //16选1
            int specialBall = getABall(16,null);
    
            //拼接字符串
            String hm = "";
            for(int i=0;i<6;i++){
                String num = exist[i] < 10 ? "0"+exist[i] : ""+ exist[i];
                if(i==0){
                    hm+= num;
                }else{
                    hm+= ","+ num;
                }
            }
            hm += "	"+(specialBall<10? "0"+specialBall:""+specialBall);
            return hm;
        }
    
        /**
         * 摇出一个号码
         * @param total 球总数
         * @param exist 已经选出的球
         * @return 一个新号码
         */
        private static int getABall(int total,int[] exist){
            Random random = new Random();
            int ball = random.nextInt(total)+1;
            while(true){
                if(contains(exist,ball)){
                    ball = random.nextInt(total)+1;
                }else{
                    break; //取到了新球,结束
                }
            }
            return ball;
        }
    
        /**
         * 判断数组是否包含某个元素
         * @param array 数组
         * @param value 元素
         * @return 是否存在
         */
        private static boolean contains(int[] array,int value){
            boolean c = false;
            if(array == null){
                return false;
            }
            for(int i=0;i<array.length;i++){
                if(array[i] == value){
                    c = true;
                    break;
                }
            }
            return c;
        }
    
        /**
         * 排序
         * @param array 数组
         */
        private static void sort(int[] array){
            for(int i=0;i< array.length-1;i++){ //比多少次
                for(int j= i+1;j<array.length;j++){ //每次循环,将最小数提前
                    if(array[i]>array[j]){ //小数冒泡
                        int tmp = array[i];
                        array[i] = array[j];
                        array[j] = tmp;
                    }
                }
            }
        }
    }

    二、结果

    07,09,12,21,28,32 14
    13,18,19,27,31,32 12
    01,02,05,16,19,25 14
    01,04,06,19,23,33 16
    09,10,11,16,28,33 10

  • 相关阅读:
    MySQL事务
    docker搭建mysql:5.7.29
    Docker构建常用PHP扩展
    Hadoop HDFS概述
    一个简单连接池的实现
    SpringCloud学习笔记-Eureka搭建过程中的出现问题
    记录Spring整合Mybatis过程中出现Invalid bound statement (not found)的解决问题思路
    项目中错误设置max-http-header-size参数导致内存激增
    DBeaver中如何调整SQL编辑器的字体大小
    Java 排序异常 Comparison method violates its general contract
  • 原文地址:https://www.cnblogs.com/hdwang/p/6269983.html
Copyright © 2020-2023  润新知