Lottery 彩票中奖

Stella981
• 阅读 467

Lottery   彩票中奖

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Ruanmou.Advaned.Lottery
{
    /// <summary>
    /// 双色球:投注号码由6个红色球号码和1个蓝色球号码组成。
    /// 红色球号码从01--33中选择
    /// 蓝色球号码从01--16中选择
    /// </summary>
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.btnStop.Enabled = false;
        }

        #region
        /// <summary>
        /// 红球集合
        /// </summary>
        private string[] RedNums =
        {
            "01","02","03","04","05","06","07","08","09","10",
            "11","12","13","14","15","16","17","18","19","20",
            "21","22","23","24","25","26","27","28","29","30",
            "31","32","33"
        };

        /// <summary>
        /// 蓝球集合
        /// </summary>
        private string[] BlueNums =
        {
            "01","02","03","04","05","06","07","08","09","10",
            "11","12","13","14","15","16"
        };
        private bool isGoon = true;
        #endregion

        /// <summary>
        /// 开始
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnStart_Click(object sender, EventArgs e)
        {
            try
            {
                this.btnStart.Text = "开始ing";
                this.btnStart.Enabled = false;
                this.isGoon = true;
                this.lblBlue.Text = "00";
                this.lblRed1.Text = "00";
                this.lblRed2.Text = "00";
                this.lblRed3.Text = "00";
                this.lblRed4.Text = "00";
                this.lblRed5.Text = "00";
                this.lblRed6.Text = "00";
                Thread.Sleep(1000);
                this.btnStop.Enabled = true;//合适打开

                List<Task> taskList = new List<Task>();
                TaskFactory taskFactory = new TaskFactory();
                foreach (Control c in this.gboSSQ.Controls)
                {
                    if (c is Label)
                    {
                        Label lbl = (Label)c;
                        taskList.Add(taskFactory.StartNew(() =>
                        {
                            while (isGoon)
                            {
                                string text = this.GetNum(lbl);//获取num
                                this.UpdateLbl(lbl, text);//更新界面
                            }
                            Console.WriteLine("线程完成");
                        }));
                    }
                }

                taskFactory.ContinueWhenAll(taskList.ToArray(), t =>
                {
                    MessageShow();
                });
            }
            catch (Exception ex)
            {
                Console.WriteLine("双色球出现异常:{0}", ex.Message);
            }
        }

        /// <summary>
        /// 结束
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnStop_Click(object sender, EventArgs e)
        {
            this.btnStop.Enabled = false;
            this.btnStart.Enabled = true;
            btnStart.Text = "开始";
            this.isGoon = false;

        }

        /// <summary>
        /// 打印结果
        /// </summary>
        private void MessageShow()
        {
            MessageBox.Show(string.Format("本期双色球结果是 {0} {1} {2} {3} {4} {5}  {6}",
                   lblRed1.Text, lblRed2.Text, lblRed3.Text, lblRed4.Text, lblRed5.Text, lblRed6.Text, lblBlue.Text));
        }

        private static object GetNum_Lock = new object();

        /// <summary>
        /// 获取num
        /// </summary>
        /// <param name="label"></param>
        /// <returns></returns>
        private string GetNum(Label label)
        {
            Thread.Sleep(1000);//让电脑轻松一点,可以随意切换线程
            if (label.Name.Equals("lblBlue"))
            {
                return this.BlueNums[new Random().Next(0, this.BlueNums.Length - 1)];//0到15
            }
            else
            {
                //33选6
                string text = this.RedNums[new Random().Next(0, this.RedNums.Length - 1)];//0到32
                lock (GetNum_Lock)
                {
                    List<string> usedNumList = this.GetUsedNums();
                    while (usedNumList.Contains(text))
                    {
                        text = this.RedNums[new Random().Next(0, this.RedNums.Length - 1)];//0到32
                    }
                    return text;
                }

            }
        }

        /// <summary>
        /// 获取当前界面上的球号码
        /// </summary>
        /// <returns></returns>
        private List<string> GetUsedNums()
        {
            List<string> usedNumList = new List<string>();
            foreach (Control c in this.gboSSQ.Controls)
            {
                if (c is Label && ((Label)c).Name.Contains("Red"))
                {
                    usedNumList.Add(((Label)c).Text);
                }
            }
            return usedNumList;
        }

        /// <summary>
        /// 更新界面
        /// </summary>
        /// <param name="lbl"></param>
        /// <param name="text"></param>
        private void UpdateLbl(Label lbl, string text)
        {
            if (lbl.InvokeRequired)
            {
                this.Invoke(new Action(() => lbl.Text = text));//交给UI线程去更新
            }
            else
            {
                lbl.Text = text;
            }
        }

    }
}

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Ruanmou.Advaned.Lottery
{
    /// <summary>
    /// 双色球:投注号码由6个红色球号码和1个蓝色球号码组成。
    /// 红色球号码从01--33中选择
    /// 蓝色球号码从01--16中选择
    /// </summary>
    public partial class frmSSQ : Form
    {
        public frmSSQ()
        {
            InitializeComponent();

            this.btnStop.Enabled = false;
        }

        #region
        /// <summary>
        /// 红球集合
        /// </summary>
        private string[] RedNums =
        {
            "01","02","03","04","05","06","07","08","09","10",
            "11","12","13","14","15","16","17","18","19","20",
            "21","22","23","24","25","26","27","28","29","30",
            "31","32","33"
        };

        /// <summary>
        /// 蓝球集合
        /// </summary>
        private string[] BlueNums =
        {
            "01","02","03","04","05","06","07","08","09","10",
            "11","12","13","14","15","16"
        };

        private static object Start_Lock = new object();
        private bool IsGoon = true;
        #endregion



        /// <summary>
        /// 点击开始:
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnStart_Click(object sender, EventArgs e)
        {
            try
            {
                Stopwatch watch = new Stopwatch();
                watch.Start();

                this.btnStart.Text = "开始ing";
                this.btnStart.Enabled = false;
                this.IsGoon = true;
                this.lblBlue.Text = "00";
                this.lblRed1.Text = "00";
                this.lblRed2.Text = "00";
                this.lblRed3.Text = "00";
                this.lblRed4.Text = "00";
                this.lblRed5.Text = "00";
                this.lblRed6.Text = "00";
                Thread.Sleep(1000);

                TaskFactory taskFactory = new TaskFactory();
                List<Task> taskList = new List<Task>();
                foreach (var item in this.gboSSQ.Controls)
                {
                    if (item is Label)
                    {
                        Label lable = (Label)item;
                        taskList.Add(taskFactory.StartNew(() =>
                             {
                                 try
                                 {
                                     while (this.IsGoon)
                                     {
                                         this.ChangeBall(lable);
                                     }
                                 }
                                 catch (Exception ex)
                                 {
                                     Console.WriteLine(ex.Message);
                                 }
                             }));
                    }
                }
                this.btnStop.Enabled = true;

                taskFactory.ContinueWhenAll(taskList.ToArray(), tList =>
                    {
                        watch.Stop();
                        Console.WriteLine("耗时{0}ms", watch.ElapsedMilliseconds);
                        this.ShowResult();
                    });

            }
            catch (Exception ex)
            {
                Console.WriteLine("双色球启动出现异常:{0}", ex.Message);
            }
        }

        #region btnStart_Click Private
        /// <summary>
        /// 
        /// </summary>
        /// <param name="lable"></param>
        private void ChangeBall(Label lable)
        {
            Thread.Sleep(1000);//可以把if else 委托出去
            if (lable.Name.Contains("Blue"))
            {
                int index = this.GetRandom(0, this.BlueNums.Length);
                string ball = this.BlueNums[index];
                this.UpdateLable(lable, ball);
            }
            else if (lable.Name.Contains("Red"))
            {
                //多个线程同时随机在33个数里面取一个,一定是有几率重复的,如果保证不重复:
                //1 白开水:分6组,线程在组里获取,然后换组:    这里存在作弊
                //  微笑刺客:换组的时候,重新随机6个组
                //2 化蝶:把33个数字都随机存到队列中   拿头上的数据,然后放回原来的
                //3 曾经沧海: 放到数组 lock
                int index = this.GetRandom(0, this.RedNums.Length);
                string ball = this.RedNums[index];

                lock (Start_Lock)
                {
                    List<string> usedNumberList = this.GetUsedRed();
                    if (usedNumberList.Contains(ball))
                    {
                        //被用了就算了
                    }
                    else
                    {
                        this.UpdateLable(lable, ball);
                    }
                }
            }

        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        private List<string> GetUsedRed()
        {
            List<string> usedNumberList = new List<string>();
            foreach (var lableSSQ in this.gboSSQ.Controls)
            {
                if (lableSSQ is Label && ((Label)lableSSQ).Name.Contains("Red"))
                {
                    usedNumberList.Add(((Label)lableSSQ).Text);
                }
            }
            return usedNumberList;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="lable"></param>
        /// <param name="number"></param>
        private void UpdateLable(Label lable, string number)
        {
            this.Invoke(new Action(() => lable.Text = number));
        }

        #endregion btnStart_Click Private

        /// <summary>
        /// 点击结束:
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnStop_Click(object sender, EventArgs e)
        {
            this.btnStart.Enabled = true;
            this.btnStop.Enabled = false;
            this.IsGoon = false;
            //this.ShowResult();

        }

        /// <summary>
        /// 
        /// </summary>
        private void ShowResult()
        {
            MessageBox.Show(string.Format("本期双色球结果为:{0} {1} {2} {3} {4} {5}  蓝球{6}"
                , this.lblRed1.Text
                , this.lblRed2.Text
                , this.lblRed3.Text
                , this.lblRed4.Text
                , this.lblRed5.Text
                , this.lblRed6.Text
                , this.lblBlue.Text));
        }

        #region random 分组
        /// <summary>
        /// 6个list string
        /// </summary>
        /// <returns></returns>
        private List<List<string>> Group()
        {
            List<List<string>> listList = new List<List<string>>()
            {
                new List<string>(),
                new List<string>(),
                new List<string>(),
                new List<string>(),
                new List<string>(),
                new List<string>()
            };
            for (int i = 0; i < this.RedNums.Length; i++)
            {
                int num = this.GetRandom(0, 1000);
                int groupNum = num % 6;
                if (listList[groupNum].Count() == 6)
                    i--;
                else
                    listList[groupNum].Add(this.RedNums[i]);
            }
            return listList;
        }
        #endregion


        #region Random 扩展
        /// <summary>
        /// 扩展的random方法
        /// </summary>
        /// <param name="min">可以获取到</param>
        /// <param name="max">可以获取到max-1</param>
        /// <returns></returns>
        private int GetRandom(int min, int max)
        {
            Guid guid = Guid.NewGuid();
            string sGuid = guid.ToString();
            int seed = DateTime.Now.Millisecond;
            for (int i = 0; i < sGuid.Length; i++)
            {
                switch (sGuid[i])
                {
                    case 'a':
                    case 'b':
                    case 'c':
                    case 'd':
                    case 'e':
                    case 'f':
                    case 'g':
                        seed = seed + 1;
                        break;
                    case 'h':
                    case 'i':
                    case 'j':
                    case 'k':
                    case 'l':
                    case 'm':
                    case 'n':
                        seed = seed + 2;
                        break;
                    case 'o':
                    case 'p':
                    case 'q':
                    case 'r':
                    case 's':
                    case 't':
                        seed = seed + 3;
                        break;
                    case 'u':
                    case 'v':
                    case 'w':
                    case 'x':
                    case 'y':
                    case 'z':
                        seed = seed + 3;
                        break;
                    default:
                        seed = seed + 4;
                        break;
                }
            }
            Random random = new Random(seed);
            return random.Next(min, max);
        }
        #endregion
    }
}
点赞
收藏
评论区
推荐文章
blmius blmius
3年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
Jacquelyn38 Jacquelyn38
3年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
待兔 待兔
2个月前
手写Java HashMap源码
HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22
Wesley13 Wesley13
2年前
Java获得今日零时零分零秒的时间(Date型)
publicDatezeroTime()throwsParseException{    DatetimenewDate();    SimpleDateFormatsimpnewSimpleDateFormat("yyyyMMdd00:00:00");    SimpleDateFormatsimp2newS
Stella981 Stella981
2年前
Android So动态加载 优雅实现与原理分析
背景:漫品Android客户端集成适配转换功能(基于目标识别(So库35M)和人脸识别库(5M)),导致apk体积50M左右,为优化客户端体验,决定实现So文件动态加载.!(https://oscimg.oschina.net/oscnet/00d1ff90e4b34869664fef59e3ec3fdd20b.png)点击上方“蓝字”关注我
Wesley13 Wesley13
2年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Wesley13 Wesley13
2年前
00:Java简单了解
浅谈Java之概述Java是SUN(StanfordUniversityNetwork),斯坦福大学网络公司)1995年推出的一门高级编程语言。Java是一种面向Internet的编程语言。随着Java技术在web方面的不断成熟,已经成为Web应用程序的首选开发语言。Java是简单易学,完全面向对象,安全可靠,与平台无关的编程语言。
Stella981 Stella981
2年前
Django中Admin中的一些参数配置
设置在列表中显示的字段,id为django模型默认的主键list_display('id','name','sex','profession','email','qq','phone','status','create_time')设置在列表可编辑字段list_editable
Wesley13 Wesley13
2年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Python进阶者 Python进阶者
7个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这