6 Replies - 446 Views - Last Post: 28 September 2012 - 06:54 PM Rate Topic: -----

#1 Gleave  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 46
  • Joined: 06-December 10

Solving the lottery with C#, this should be fun!

Posted 24 September 2012 - 08:50 AM

So I've been working on this app for the Ohio Rolling Cash 5 where you have picks between the numbers of 1 and 39. You get to pick 5 numbers, so odds for jackpot are 1 in 575,757...which compared to odds of Powerball (1 in 175 million!) and those of the sort is very good. I have searched for various methods like odd/even count, hot cold numbers and what not and formulated my own method of trying to guess these five numbers based on what day it is. One error that I get is sometimes when I click the "Generate" button, the program freezes and doesn't give numbers. The only way to stop the app is to force close it. If anyone sees ways I could improve this I would be happy to hear your opinion, and heck if you help me win I would share the wealth for the knowledge. I hard-coded all of the controls, so you won't need to edit the designer, and you can delete the "InitializeComponent()" line of code. There is a text file(attached) that has the day of the drawings along with the drawings for that day. They are separated by commas so that I could format them when I read in the data. Just change the path to wherever you download the file to from line
"StreamReader sr = new StreamReader(@"C:\Users\Technology\Desktop\cashfivelotto.txt");".

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.IO;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace OhioRollingCash5App
{
    public partial class frmMain : Form
    {
        private TextBox tbmain = new TextBox();

        private Button bgen = new Button();
        private Button bclear = new Button();

        private Label lblpickone = new Label();
        private Label lblpicktwo = new Label();
        private Label lblpickthree = new Label();
        private Label lblpickfour = new Label();
        private Label lblpickfive = new Label();

        private Label lblone = new Label();
        private Label lbltwo = new Label();
        private Label lblthree = new Label();
        private Label lblfour = new Label();
        private Label lblfive = new Label();

        private RadioButton rMon = new RadioButton();
        private RadioButton rTue = new RadioButton();
        private RadioButton rWed = new RadioButton();
        private RadioButton rThu = new RadioButton();
        private RadioButton rFri = new RadioButton();
        private RadioButton rSat = new RadioButton();
        private RadioButton rSun = new RadioButton();

        //vars for each actual number
        //these are used to hold the numbers
        //actual value and other data about 
        //the number
        List<Number> allnumobjs = new List<Number>();

        //number priority lists
        List<int> hotnums = new List<int>();
        List<int> midnum = new List<int>();
        List<int> coldnums = new List<int>();

        //list to hold the picks
        List<int> allpicks = new List<int>();

        //List<int> poolone = new List<int>();
        //List<int> pooltwo = new List<int>();

        public frmMain()
        {
            #region properties

            //size of the form
            Size = new Size(638, 645);
            //form title
            this.Text = "Rolling Cash 5";

            //button properties
            bgen.Location = new Point(262, 45);
            bgen.Width = 88;
            bgen.Height = 41;
            bgen.Text = "Generate";

            bclear.Location = new Point(382, 45);
            bclear.Width = 88;
            bclear.Height = 41;
            bclear.Text = "Clear";

            //textbox properties
            tbmain.Width = 202;
            tbmain.Height = 513;
            tbmain.Location = new Point(29, 45);
            tbmain.Multiline = true;
            tbmain.ReadOnly = true;
            tbmain.ScrollBars = ScrollBars.Vertical;
            tbmain.BackColor = Color.White;

            //label locations
            //label sizes, generic
            Size lblSize = new Size(61, 21);

            lblone. Location = new Point(258, 113);
            lblone.Size = lblSize;
            lblone.Font = new Font("Times New Roman", 10.8f, FontStyle.Regular);
            lblone.Text = "Pick 1:";

            lbltwo.Location = new Point(258, 163);
            lbltwo.Size = lblSize;
            lbltwo.Font = new Font("Times New Roman", 10.8f, FontStyle.Regular);
            lbltwo.Text = "Pick 2:";

            lblthree.Location = new Point(258, 213);
            lblthree.Size = lblSize;
            lblthree.Font = new Font("Times New Roman", 10.8f, FontStyle.Regular);
            lblthree.Text = "Pick 3:";

            lblfour.Location = new Point(258, 263);
            lblfour.Size = lblSize;
            lblfour.Font = new Font("Times New Roman", 10.8f, FontStyle.Regular);
            lblfour.Text = "Pick 4:";

            lblfive.Location = new Point(258, 313);
            lblfive.Size = lblSize;
            lblfive.Font = new Font("Times New Roman", 10.8f, FontStyle.Regular);
            lblfive.Text = "Pick 5:";

            //display labels
            lblpickone.Location = new Point(350, 113);
            lblpickone.Size = lblSize;
            lblpickone.Font = new Font("Times New Roman", 10.8f, FontStyle.Regular);

            lblpicktwo.Location = new Point(350, 163);
            lblpicktwo.Size = lblSize;
            lblpicktwo.Font = new Font("Times New Roman", 10.8f, FontStyle.Regular);

            lblpickthree.Location = new Point(350, 213);
            lblpickthree.Size = lblSize;
            lblpickthree.Font = new Font("Times New Roman", 10.8f, FontStyle.Regular);

            lblpickfour.Location = new Point(350, 263);
            lblpickfour.Size = lblSize;
            lblpickfour.Font = new Font("Times New Roman", 10.8f, FontStyle.Regular);

            lblpickfive.Location = new Point(350, 313);
            lblpickfive.Size = lblSize;
            lblpickfive.Font = new Font("Times New Roman", 10.8f, FontStyle.Regular);

            //radio button size generic
            Size rsize = new Size(120, 26);

            //radio button properties
            rMon.Location = new Point(252, 403);
            rMon.Size = rsize;
            rMon.Font = new Font("Times New Roman", 10.8f, FontStyle.Regular);
            rMon.Text = "Monday";

            rTue.Location = new Point(373, 403);
            rTue.Size = rsize;
            rTue.Font = new Font("Times New Roman", 10.8f, FontStyle.Regular);
            rTue.Text = "Tuesday";

            rWed.Location = new Point(494, 403);
            rWed.Size = rsize;
            rWed.Font = new Font("Times New Roman", 10.8f, FontStyle.Regular);
            rWed.Text = "Wednesday";

            rThu.Location = new Point(252, 469);
            rThu.Size = rsize;
            rThu.Font = new Font("Times New Roman", 10.8f, FontStyle.Regular);
            rThu.Text = "Thursday";

            rFri.Location = new Point(373, 469);
            rFri.Size = rsize;
            rFri.Font = new Font("Times New Roman", 10.8f, FontStyle.Regular);
            rFri.Text = "Friday";

            rSat.Location = new Point(494, 469);
            rSat.Size = rsize;
            rSat.Font = new Font("Times New Roman", 10.8f, FontStyle.Regular);
            rSat.Text = "Saturday";

            rSun.Location = new Point(373, 535);
            rSun.Size = rsize;
            rSun.Font = new Font("Times New Roman", 10.8f, FontStyle.Regular);
            rSun.Text = "Sunday";
            #endregion

            #region events
            //register event handles
            bgen.Click += new EventHandler(bgen_Click);
            bclear.Click += new EventHandler(bclear_Click);
            #endregion

            #region numobjs
            //  DateTime dt = new DateTime();

            Number n1 = new Number();
            Number n2 = new Number();
            Number n3 = new Number();
            Number n4 = new Number();
            Number n5 = new Number();
            Number n6 = new Number();
            Number n7 = new Number();
            Number n8 = new Number();
            Number n9 = new Number();
            Number n10 = new Number();
            Number n11 = new Number();
            Number n12 = new Number();
            Number n13 = new Number();
            Number n14 = new Number();
            Number n15 = new Number();
            Number n16 = new Number();
            Number n17 = new Number();
            Number n18 = new Number();
            Number n19 = new Number();
            Number n20 = new Number();
            Number n21 = new Number();
            Number n22 = new Number();
            Number n23 = new Number();
            Number n24 = new Number();
            Number n25 = new Number();
            Number n26 = new Number();
            Number n27 = new Number();
            Number n28 = new Number();
            Number n29 = new Number();
            Number n30 = new Number();
            Number n31 = new Number();
            Number n32 = new Number();
            Number n33 = new Number();
            Number n34 = new Number();
            Number n35 = new Number();
            Number n36 = new Number();
            Number n37 = new Number();
            Number n38 = new Number();
            Number n39 = new Number();

            //put all num objects into a list for help
            //with processing
            allnumobjs.Add(n1);
            allnumobjs.Add(n2);
            allnumobjs.Add(n3);
            allnumobjs.Add(n4);
            allnumobjs.Add(n5);
            allnumobjs.Add(n6);
            allnumobjs.Add(n7);
            allnumobjs.Add(n8);
            allnumobjs.Add(n9);
            allnumobjs.Add(n10);
            allnumobjs.Add(n11);
            allnumobjs.Add(n12);
            allnumobjs.Add(n13);
            allnumobjs.Add(n14);
            allnumobjs.Add(n15);
            allnumobjs.Add(n16);
            allnumobjs.Add(n17);
            allnumobjs.Add(n18);
            allnumobjs.Add(n19);
            allnumobjs.Add(n20);
            allnumobjs.Add(n21);
            allnumobjs.Add(n22);
            allnumobjs.Add(n23);
            allnumobjs.Add(n24);
            allnumobjs.Add(n25);
            allnumobjs.Add(n26);
            allnumobjs.Add(n27);
            allnumobjs.Add(n28);
            allnumobjs.Add(n29);
            allnumobjs.Add(n30);
            allnumobjs.Add(n31);
            allnumobjs.Add(n32);
            allnumobjs.Add(n33);
            allnumobjs.Add(n34);
            allnumobjs.Add(n35);
            allnumobjs.Add(n36);
            allnumobjs.Add(n37);
            allnumobjs.Add(n38);
            allnumobjs.Add(n39);
            
            //absolute values for each number
            //or...its own value
            n1.A_Val = 1;
            n2.A_Val = 2;
            n3.A_Val = 3;
            n4.A_Val = 4;
            n5.A_Val = 5;
            n6.A_Val = 6;
            n7.A_Val = 7;
            n8.A_Val = 8;
            n9.A_Val = 9;
            n10.A_Val = 10;
            n11.A_Val = 11;
            n12.A_Val = 12;
            n13.A_Val = 13;
            n14.A_Val = 14;
            n15.A_Val = 15;
            n16.A_Val = 16;
            n17.A_Val = 17;
            n18.A_Val = 18;
            n19.A_Val = 19;
            n20.A_Val = 20;
            n21.A_Val = 21;
            n22.A_Val = 22;
            n23.A_Val = 23;
            n24.A_Val = 24;
            n25.A_Val = 25;
            n26.A_Val = 26;
            n27.A_Val = 27;
            n28.A_Val = 28;
            n29.A_Val = 29;
            n30.A_Val = 30;
            n31.A_Val = 31;
            n32.A_Val = 32;
            n33.A_Val = 33;
            n34.A_Val = 34;
            n35.A_Val = 35;
            n36.A_Val = 36;
            n37.A_Val = 37;
            n38.A_Val = 38;
            n39.A_Val = 39;

            //odd/even values
            n1.Is_Odd = true;
            n1.Is_Even = false;
            n2.Is_Odd = false;
            n2.Is_Even = true;
            n3.Is_Odd = true;
            n3.Is_Even = false;
            n4.Is_Odd = false;
            n4.Is_Even = true;
            n5.Is_Odd = true;
            n5.Is_Even = false;
            n6.Is_Odd = false;
            n6.Is_Even = true;
            n7.Is_Odd = true;
            n7.Is_Even = false;
            n8.Is_Odd = false;
            n8.Is_Even = true;
            n9.Is_Odd = true;
            n9.Is_Even = false;
            n10.Is_Odd = false;
            n10.Is_Even = true;
            n11.Is_Odd = true;
            n11.Is_Even = false;
            n12.Is_Odd = false;
            n12.Is_Even = true;
            n13.Is_Odd = true;
            n13.Is_Even = false;
            n14.Is_Odd = false;
            n14.Is_Even = true;
            n15.Is_Odd = true;
            n15.Is_Even = false;
            n16.Is_Odd = false;
            n16.Is_Even = true;
            n17.Is_Odd = true;
            n17.Is_Even = false;
            n18.Is_Odd = false;
            n18.Is_Even = true;
            n19.Is_Odd = true;
            n19.Is_Even = false;
            n20.Is_Odd = false;
            n20.Is_Even = true;
            n21.Is_Odd = true;
            n21.Is_Even = false;
            n22.Is_Odd = false;
            n22.Is_Even = true;
            n23.Is_Odd = true;
            n23.Is_Even = false;
            n24.Is_Odd = false;
            n24.Is_Even = true;
            n25.Is_Odd = true;
            n25.Is_Even = false;
            n26.Is_Odd = false;
            n26.Is_Even = true;
            n27.Is_Odd = true;
            n27.Is_Even = false;
            n28.Is_Odd = false;
            n28.Is_Even = true;
            n29.Is_Odd = true;
            n29.Is_Even = false;
            n30.Is_Odd = false;
            n30.Is_Even = true;
            n31.Is_Odd = true;
            n31.Is_Even = false;
            n32.Is_Odd = false;
            n32.Is_Even = true;
            n33.Is_Odd = true;
            n33.Is_Even = false;
            n34.Is_Odd = false;
            n34.Is_Even = true;
            n35.Is_Odd = true;
            n35.Is_Even = false;
            n36.Is_Odd = false;
            n36.Is_Even = true;
            n37.Is_Odd = true;
            n37.Is_Even = false;
            n38.Is_Odd = false;
            n38.Is_Even = true;
            n39.Is_Odd = true;
            n39.Is_Even = false;
            #endregion

            #region readintext

            //reading in the file
            int counter = 0;
            string line;

            //list to add the results to
            List<string> ipicks = new List<string>();

            //read in results
            StreamReader sr = new StreamReader(@"C:\Users\Technology\Desktop\cashfivelotto.txt");

            //iterate through the text file, until we reach the end
            while ((line = sr.ReadLine()) != null)
            {
                //remove the comma's from the file and store the numbers in numset 
                char[] delimiters = new char[] { ',' };
                string[] numset = line.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);

                for (int i = 0; i < numset.Length; i++)
                {
                    //counter for line numbers and formatting 
                    counter++;

                    if (counter % 6 == 0)
                    {
                        //add the text 
                        tbmain.Text += line + System.Environment.NewLine;

                        //enumerate values for each number
                        //based on netvalues and day values
                        for (int j = 1; j < numset.Length; j++)
                        {
                            foreach(Number n in allnumobjs)
                            {
                                if(n.A_Val == int.Parse(numset[j]))
                                {
                                    n.Net_Val += 1;

                                    if (numset[0] == "MON")
                                    {
                                        n.Monday += 1;
                                    }
                                    else if (numset[0] == "TUE")
                                    {
                                        n.Tuesday += 1;
                                    }
                                    else if (numset[0] == "WED")
                                    {
                                        n.Wednesday += 1;
                                    }
                                    else if (numset[0] == "THU")
                                    {
                                        n.Thursday += 1;
                                    }
                                    else if (numset[0] == "FRI")
                                    {
                                        n.Friday += 1;
                                    }
                                    else if (numset[0] == "SAT")
                                    {
                                        n.Saturday += 1;
                                    }
                                    else
                                    {
                                        n.Sunday += 1;
                                    }
                                }
                            }
                        }
                    }
                    ipicks.Add(numset[i]);
                }
                //sr.Close();
            }
            //close streamreader
            sr.Close();
            #endregion

            #region lists

            int average = 0;

            foreach (Number n in allnumobjs)
            {
                average += n.A_Val;
                if (n.Net_Val >= 23)
                {
                    hotnums.Add(n.A_Val);
                }
                else if (n.Net_Val <= 22)
                {
                    coldnums.Add(n.A_Val);
                }

            }

            average = average / allnumobjs.Count;

            //eliminate doubles from the lists
            foreach (int i in coldnums)
            {
                for (int x = 0; x < hotnums.Count; x++)
                {
                    if (i == hotnums[x])
                    {
                        hotnums.Remove(x);
                    }
                }
            }

           // lblpickone.Text = hotnums.Count.ToString();
           // lblpickfive.Text = coldnums.Count.ToString();
     
            #endregion

            #region addcontrols
            //add the controls to the form
            Controls.Add(tbmain);

            Controls.Add(bgen);
            Controls.Add(bclear);

            Controls.Add(lblone);
            Controls.Add(lbltwo);
            Controls.Add(lblthree);
            Controls.Add(lblfour);
            Controls.Add(lblfive);

            Controls.Add(lblpickone);
            Controls.Add(lblpicktwo);
            Controls.Add(lblpickthree);
            Controls.Add(lblpickfour);
            Controls.Add(lblpickfive);

            Controls.Add(rMon);
            Controls.Add(rTue);
            Controls.Add(rWed);
            Controls.Add(rThu);
            Controls.Add(rFri);
            Controls.Add(rSat);
            Controls.Add(rSun);

            #endregion

            //InitializeComponent();
        }

        private void bgen_Click(object sender, EventArgs e)
        {
            //day lists for common numbers for the day
            List<int> mon = new List<int>();
            List<int> tue = new List<int>();
            List<int> wed = new List<int>();
            List<int> thu = new List<int>();
            List<int> fri = new List<int>();
            List<int> sat = new List<int>();
            List<int> sun = new List<int>();

            int pick1 = 0, pick2 = 0, pick3 = 0, pick4 = 0, pick5 = 0;

            if (rMon.Checked)
            {
                foreach (Number n in this.allnumobjs)
                {
                    if (n.Monday > 3)
                    {
                        mon.Add(n.A_Val);
                    }
                }

            rand1:
                 Random r = new Random();

                 pick1 = hotnums[r.Next(0, hotnums.Count)];

                 pick2 = hotnums[r.Next(0, hotnums.Count)];

                 if (pick1 == pick2)
                 {
                     goto rand1;
                 }

                 pick3 = mon[r.Next(0, mon.Count)];

                 pick4 = mon[r.Next(0, mon.Count)];

                 if (pick3 == pick4)
                 {
                     goto rand1;
                 }

                 pick5 = coldnums[r.Next(0, coldnums.Count)];

                 int picktot = pick1 + pick2 + pick3 + pick4 + pick5;

                 if ((picktot <= 80) || (picktot >= 125))
                 {
                     goto rand1;
                 }

                 allpicks.Add(pick1);
                 allpicks.Add(pick2);
                 allpicks.Add(pick3);
                 allpicks.Add(pick4);
                 allpicks.Add(pick5);

                 allpicks.Sort();

                 int even = 0;
                 int odd = 0;

                 foreach (int i in allpicks)
                 {
                     foreach (Number n in allnumobjs)
                     {
                         if (i == n.A_Val)
                         {
                             if (n.Is_Even)
                             {
                                 even++;
                             }
                             else
                             {
                                 odd++;
                             }
                         }
                     }
                 }

                //make sure we keep 4/1, 3/2 split on odds and evens
                 if ((even > 4) || (odd > 4))
                 {
                     goto rand1;
                 }
                //first pick shouldnt be more than 14
                 if (allpicks[0] >= 14)
                 {
                     goto rand1;
                 }

                //the picks are good, display them
                 lblpickone.Text = allpicks[0].ToString();
                 lblpicktwo.Text = allpicks[1].ToString();
                 lblpickthree.Text = allpicks[2].ToString();
                 lblpickfour.Text = allpicks[3].ToString();
                 lblpickfive.Text = allpicks[4].ToString();

                 allpicks.Clear();
                 mon.Clear();
            }
            else if (rTue.Checked)
            {
                foreach (Number n in this.allnumobjs)
                {
                    if (n.Tuesday > 3)
                    {
                        tue.Add(n.A_Val);
                    }
                }

            rand2:
                Random r = new Random();

                pick1 = hotnums[r.Next(0, hotnums.Count)];

                pick2 = hotnums[r.Next(0, hotnums.Count)];

                if (pick1 == pick2)
                {
                    goto rand2;
                }

                pick3 = tue[r.Next(0, tue.Count)];

                pick4 = tue[r.Next(0, tue.Count)];

                if (pick3 == pick4)
                {
                    goto rand2;
                }

                pick5 = coldnums[r.Next(0, coldnums.Count)];

                int picktot = pick1 + pick2 + pick3 + pick4 + pick5;

                if ((picktot <= 80) || (picktot >= 125))
                {
                    goto rand2;
                }

                allpicks.Add(pick1);
                allpicks.Add(pick2);
                allpicks.Add(pick3);
                allpicks.Add(pick4);
                allpicks.Add(pick5);

                allpicks.Sort();

                int even = 0;
                int odd = 0;

                foreach (int i in allpicks)
                {
                    foreach (Number n in allnumobjs)
                    {
                        if (i == n.A_Val)
                        {
                            if (n.Is_Even)
                            {
                                even++;
                            }
                            else
                            {
                                odd++;
                            }
                        }
                    }
                }

                //make sure we keep 4/1, 3/2 split on odds and evens
                if ((even > 4) || (odd > 4))
                {
                    goto rand2;
                }
                //first pick shouldnt be more than 14
                if (allpicks[0] >= 14)
                {
                    goto rand2;
                }

                //the picks are good, display them
                lblpickone.Text = allpicks[0].ToString();
                lblpicktwo.Text = allpicks[1].ToString();
                lblpickthree.Text = allpicks[2].ToString();
                lblpickfour.Text = allpicks[3].ToString();
                lblpickfive.Text = allpicks[4].ToString();

                allpicks.Clear();
                tue.Clear();
            }
            else if (rWed.Checked)
            {
                foreach (Number n in this.allnumobjs)
                {
                    if (n.Wednesday > 3)
                    {
                        wed.Add(n.A_Val);
                    }
                }

            rand3:
                Random r = new Random();

                pick1 = hotnums[r.Next(0, hotnums.Count)];

                pick2 = hotnums[r.Next(0, hotnums.Count)];

                if (pick1 == pick2)
                {
                    goto rand3;
                }

                pick3 = wed[r.Next(0, wed.Count)];

                pick4 = wed[r.Next(0, wed.Count)];

                if (pick3 == pick4)
                {
                    goto rand3;
                }

                pick5 = coldnums[r.Next(0, coldnums.Count)];

                int picktot = pick1 + pick2 + pick3 + pick4 + pick5;

                if ((picktot <= 80) || (picktot >= 125))
                {
                    goto rand3;
                }

                allpicks.Add(pick1);
                allpicks.Add(pick2);
                allpicks.Add(pick3);
                allpicks.Add(pick4);
                allpicks.Add(pick5);

                allpicks.Sort();

                int even = 0;
                int odd = 0;

                foreach (int i in allpicks)
                {
                    foreach (Number n in allnumobjs)
                    {
                        if (i == n.A_Val)
                        {
                            if (n.Is_Even)
                            {
                                even++;
                            }
                            else
                            {
                                odd++;
                            }
                        }
                    }
                }

                //make sure we keep 4/1, 3/2 split on odds and evens
                if ((even > 4) || (odd > 4))
                {
                    goto rand3;
                }
                //first pick shouldnt be more than 14
                if (allpicks[0] >= 14)
                {
                    goto rand3;
                }

                //the picks are good, display them
                lblpickone.Text = allpicks[0].ToString();
                lblpicktwo.Text = allpicks[1].ToString();
                lblpickthree.Text = allpicks[2].ToString();
                lblpickfour.Text = allpicks[3].ToString();
                lblpickfive.Text = allpicks[4].ToString();

                allpicks.Clear();
                wed.Clear();
            }
            else if (rThu.Checked)
            {
                foreach (Number n in this.allnumobjs)
                {
                    if (n.Thursday > 3)
                    {
                        thu.Add(n.A_Val);
                    }
                }

            rand4:
                Random r = new Random();

                pick1 = hotnums[r.Next(0, hotnums.Count)];

                pick2 = hotnums[r.Next(0, hotnums.Count)];

                if (pick1 == pick2)
                {
                    goto rand4;
                }

                pick3 = thu[r.Next(0, thu.Count)];

                pick4 = thu[r.Next(0, thu.Count)];

                if (pick3 == pick4)
                {
                    goto rand4;
                }

                pick5 = coldnums[r.Next(0, coldnums.Count)];

                int picktot = pick1 + pick2 + pick3 + pick4 + pick5;

                if ((picktot <= 80) || (picktot >= 125))
                {
                    goto rand4;
                }

                allpicks.Add(pick1);
                allpicks.Add(pick2);
                allpicks.Add(pick3);
                allpicks.Add(pick4);
                allpicks.Add(pick5);

                allpicks.Sort();

                int even = 0;
                int odd = 0;

                foreach (int i in allpicks)
                {
                    foreach (Number n in allnumobjs)
                    {
                        if (i == n.A_Val)
                        {
                            if (n.Is_Even)
                            {
                                even++;
                            }
                            else
                            {
                                odd++;
                            }
                        }
                    }
                }

                //make sure we keep 4/1, 3/2 split on odds and evens
                if ((even > 4) || (odd > 4))
                {
                    goto rand4;
                }
                //first pick shouldnt be more than 14
                if (allpicks[0] >= 14)
                {
                    goto rand4;
                }

                //the picks are good, display them
                lblpickone.Text = allpicks[0].ToString();
                lblpicktwo.Text = allpicks[1].ToString();
                lblpickthree.Text = allpicks[2].ToString();
                lblpickfour.Text = allpicks[3].ToString();
                lblpickfive.Text = allpicks[4].ToString();

                allpicks.Clear();
                thu.Clear();
            }
            else if (rFri.Checked)
            {
                foreach (Number n in this.allnumobjs)
                {
                    if (n.Friday > 3)
                    {
                        fri.Add(n.A_Val);
                    }
                }

            rand5:
                Random r = new Random();

                pick1 = hotnums[r.Next(0, hotnums.Count)];

                pick2 = hotnums[r.Next(0, hotnums.Count)];

                if (pick1 == pick2)
                {
                    goto rand5;
                }

                pick3 = fri[r.Next(0, fri.Count)];

                pick4 = fri[r.Next(0, fri.Count)];

                if (pick3 == pick4)
                {
                    goto rand5;
                }

                pick5 = coldnums[r.Next(0, coldnums.Count)];

                int picktot = pick1 + pick2 + pick3 + pick4 + pick5;

                if ((picktot <= 80) || (picktot >= 125))
                {
                    goto rand5;
                }

                allpicks.Add(pick1);
                allpicks.Add(pick2);
                allpicks.Add(pick3);
                allpicks.Add(pick4);
                allpicks.Add(pick5);

                allpicks.Sort();

                int even = 0;
                int odd = 0;

                foreach (int i in allpicks)
                {
                    foreach (Number n in allnumobjs)
                    {
                        if (i == n.A_Val)
                        {
                            if (n.Is_Even)
                            {
                                even++;
                            }
                            else
                            {
                                odd++;
                            }
                        }
                    }
                }

                //make sure we keep 4/1, 3/2 split on odds and evens
                if ((even > 4) || (odd > 4))
                {
                    goto rand5;
                }
                //first pick shouldnt be more than 14
                if (allpicks[0] >= 14)
                {
                    goto rand5;
                }

                //the picks are good, display them
                lblpickone.Text = allpicks[0].ToString();
                lblpicktwo.Text = allpicks[1].ToString();
                lblpickthree.Text = allpicks[2].ToString();
                lblpickfour.Text = allpicks[3].ToString();
                lblpickfive.Text = allpicks[4].ToString();

                allpicks.Clear();
                fri.Clear();
            }
            else if (rSat.Checked)
            {
                foreach (Number n in this.allnumobjs)
                {
                    if (n.Saturday > 3)
                    {
                        sat.Add(n.A_Val);
                    }
                }

            rand6:
                Random r = new Random();

                pick1 = hotnums[r.Next(0, hotnums.Count)];

                pick2 = hotnums[r.Next(0, hotnums.Count)];

                if (pick1 == pick2)
                {
                    goto rand6;
                }

                pick3 = sat[r.Next(0, sat.Count)];

                pick4 = sat[r.Next(0, sat.Count)];

                if (pick3 == pick4)
                {
                    goto rand6;
                }

                pick5 = coldnums[r.Next(0, coldnums.Count)];

                int picktot = pick1 + pick2 + pick3 + pick4 + pick5;

                if ((picktot <= 80) || (picktot >= 125))
                {
                    goto rand6;
                }

                allpicks.Add(pick1);
                allpicks.Add(pick2);
                allpicks.Add(pick3);
                allpicks.Add(pick4);
                allpicks.Add(pick5);

                allpicks.Sort();

                int even = 0;
                int odd = 0;

                foreach (int i in allpicks)
                {
                    foreach (Number n in allnumobjs)
                    {
                        if (i == n.A_Val)
                        {
                            if (n.Is_Even)
                            {
                                even++;
                            }
                            else
                            {
                                odd++;
                            }
                        }
                    }
                }

                //make sure we keep 4/1, 3/2 split on odds and evens
                if ((even > 4) || (odd > 4))
                {
                    goto rand6;
                }
                //first pick shouldnt be more than 14
                if (allpicks[0] >= 14)
                {
                    goto rand6;
                }

                //the picks are good, display them
                lblpickone.Text = allpicks[0].ToString();
                lblpicktwo.Text = allpicks[1].ToString();
                lblpickthree.Text = allpicks[2].ToString();
                lblpickfour.Text = allpicks[3].ToString();
                lblpickfive.Text = allpicks[4].ToString();

                allpicks.Clear();
                sat.Clear();
            }
            else if (rSun.Checked)
            {
                foreach (Number n in this.allnumobjs)
                {
                    if (n.Sunday > 3)
                    {
                        sun.Add(n.A_Val);
                    }
                }

            rand7:
                Random r = new Random();

                pick1 = hotnums[r.Next(0, hotnums.Count)];

                pick2 = hotnums[r.Next(0, hotnums.Count)];

                if (pick1 == pick2)
                {
                    goto rand7;
                }

                pick3 = sun[r.Next(0, sun.Count)];

                pick4 = sun[r.Next(0, sun.Count)];

                if (pick3 == pick4)
                {
                    goto rand7;
                }

                pick5 = coldnums[r.Next(0, coldnums.Count)];

                int picktot = pick1 + pick2 + pick3 + pick4 + pick5;

                if ((picktot <= 80) || (picktot >= 125))
                {
                    goto rand7;
                }

                allpicks.Add(pick1);
                allpicks.Add(pick2);
                allpicks.Add(pick3);
                allpicks.Add(pick4);
                allpicks.Add(pick5);

                allpicks.Sort();

                int even = 0;
                int odd = 0;

                foreach (int i in allpicks)
                {
                    foreach (Number n in allnumobjs)
                    {
                        if (i == n.A_Val)
                        {
                            if (n.Is_Even)
                            {
                                even++;
                            }
                            else
                            {
                                odd++;
                            }
                        }
                    }
                }

                //make sure we keep 4/1, 3/2 split on odds and evens
                if ((even > 4) || (odd > 4))
                {
                    goto rand7;
                }
                //first pick shouldnt be more than 14
                if (allpicks[0] >= 14)
                {
                    goto rand7;
                }

                //the picks are good, display them
                lblpickone.Text = allpicks[0].ToString();
                lblpicktwo.Text = allpicks[1].ToString();
                lblpickthree.Text = allpicks[2].ToString();
                lblpickfour.Text = allpicks[3].ToString();
                lblpickfive.Text = allpicks[4].ToString();

                allpicks.Clear();
                sun.Clear();
            }
            else
            {
                MessageBox.Show("Please select a day");
            }
        }

        private void bclear_Click(object sender, EventArgs e)
        {
            lblpickone.Text = "";
            lblpicktwo.Text = "";
            lblpickthree.Text = "";
            lblpickfour.Text = "";
            lblpickfive.Text = "";
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }

    public class Number
    {
        public Number()
        {
        }

        #region properties

        public int Monday
        {
            get { return this.monday; }
            set
            {
                this.monday = value;
            }
        }
        private int monday;

        public int Tuesday
        {
            get { return this.tuesday; }
            set
            {
                this.tuesday = value;
            }
        }
        private int tuesday;
        

        public int Wednesday
        {
            get { return this.wednesday; }
            set
            {
                this.wednesday = value;
            }
        }
        private int wednesday;

        public int Thursday
        {
            get { return this.thursday; }
            set
            {
                this.thursday = value;
            }
        }
        private int thursday;

        public int Friday
        {
            get { return this.friday; }
            set
            {
                this.friday = value;
            }
        }
        private int friday;

        public int Saturday
        {
            get { return this.saturday; }
            set
            {
                this.saturday = value;
            }
        }
        private int saturday;

        public int Sunday
        {
            get { return this.sunday; }
            set
            {
                this.sunday = value;
            }
        }
        private int sunday;

        public bool Is_Even
        {
            get { return this.is_even; }

            set
            {
                this.is_even = value;
            }
        }
        private bool is_even;

        public bool Is_Odd
        {
            get { return this.is_odd; }

            set
            {
                this.is_odd = value;
            }
        }
        private bool is_odd;

        public bool Is_Hot
        {
            get { return this.is_hot; }

            set
            {
                this.is_hot = value;
            }
        }
        private bool is_hot;

        public bool Is_Cold
        {
            get { return this.is_cold; }

            set
            {
                this.is_cold = value;
            }
        }
        private bool is_cold;

        public int A_Val
        {
            get { return this.a_val; }

            set
            {
                this.a_val = value;
            }
        }
        private int a_val;

        public int Net_Val
        {
            get { return this.net_val; }

            set
            {
                this.net_val = value;
            }
        }
        private int net_val;

        #endregion
    }
}


Attached File(s)



Is This A Good Question/Topic? 0
  • +

Replies To: Solving the lottery with C#, this should be fun!

#2 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3461
  • View blog
  • Posts: 10,669
  • Joined: 05-May 12

Re: Solving the lottery with C#, this should be fun!

Posted 24 September 2012 - 01:50 PM

*
POPULAR

Do not do all your computations in the button click handler. Start another thread and do all the work there so that your UI remains responsive.

Use functions to break up large runs of code, as well as get rid of duplicate code. It'll make your code more readable.

Don't use goto's. Structure your code to use while, do-while, and for loops.
Was This Post Helpful? 5
  • +
  • -

#3 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,957
  • Joined: 21-March 08

Re: Solving the lottery with C#, this should be fun!

Posted 25 September 2012 - 04:51 AM

You could replace 200 lines of code by changing all of the "number" code(starting at line 184, ending 386) to this...

for (int i = 1; i <= 39; i++)
{
    allnumobjs.Add(new Number()
    {
       A_Val = i,
       Is_Odd = (i % 2 != 0),
       Is_Even = (i % 2 == 0)
    });
}



You never actually use all of those 'Number' objects.
Was This Post Helpful? 3
  • +
  • -

#4 Gleave  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 46
  • Joined: 06-December 10

Re: Solving the lottery with C#, this should be fun!

Posted 26 September 2012 - 09:07 AM

Thank you for your responses. Wow I didn't know you could instantiate a new number from a method call that is awesome. Now when you say don't let the button event handle all the calculations you mean putting them in methods and calling them in the button press instead of all being coded into the button?
Also is the code being in the button causing it to crash? this is what I thought in the first place but when I debug it gives no such sign of crashing. Thank you again.

This post has been edited by Gleave: 26 September 2012 - 09:17 AM

Was This Post Helpful? 0
  • +
  • -

#5 Gleave  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 46
  • Joined: 06-December 10

Re: Solving the lottery with C#, this should be fun!

Posted 26 September 2012 - 02:50 PM

Ok I took out goto's and replaced them with whiles. The program stopped freezing!!! Now the only problem is when I try to remove duplicates its not seeming to get them all. Here is updated code.

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

namespace OhioRollingCash5App
{
    public partial class frmMain : Form
    {
        private TextBox tbmain = new TextBox();

        private Button bgen = new Button();
        private Button bclear = new Button();

        private Label lblpickone = new Label();
        private Label lblpicktwo = new Label();
        private Label lblpickthree = new Label();
        private Label lblpickfour = new Label();
        private Label lblpickfive = new Label();

        private Label lblone = new Label();
        private Label lbltwo = new Label();
        private Label lblthree = new Label();
        private Label lblfour = new Label();
        private Label lblfive = new Label();

        private RadioButton rMon = new RadioButton();
        private RadioButton rTue = new RadioButton();
        private RadioButton rWed = new RadioButton();
        private RadioButton rThu = new RadioButton();
        private RadioButton rFri = new RadioButton();
        private RadioButton rSat = new RadioButton();
        private RadioButton rSun = new RadioButton();

        //vars for each actual number
        //these are used to hold the numbers
        //actual value and other data about 
        //the number
        List<Number> allnumobjs = new List<Number>();

        //number priority lists
        List<int> hotnums = new List<int>();
        List<int> midnum = new List<int>();
        List<int> coldnums = new List<int>();

        //list to hold the picks
        List<int> allpicks = new List<int>();

        //List<int> poolone = new List<int>();
        //List<int> pooltwo = new List<int>();

        public frmMain()
        {
            #region properties

            //size of the form
            Size = new Size(638, 645);
            //form title
            this.Text = "Rolling Cash 5";

            //button properties
            bgen.Location = new Point(262, 45);
            bgen.Width = 88;
            bgen.Height = 41;
            bgen.Text = "Generate";

            bclear.Location = new Point(382, 45);
            bclear.Width = 88;
            bclear.Height = 41;
            bclear.Text = "Clear";

            //textbox properties
            tbmain.Width = 202;
            tbmain.Height = 513;
            tbmain.Location = new Point(29, 45);
            tbmain.Multiline = true;
            tbmain.ReadOnly = true;
            tbmain.ScrollBars = ScrollBars.Vertical;
            tbmain.BackColor = Color.White;

            //label locations
            //label sizes, generic
            Size lblSize = new Size(61, 21);

            lblone. Location = new Point(258, 113);
            lblone.Size = lblSize;
            lblone.Font = new Font("Times New Roman", 10.8f, FontStyle.Regular);
            lblone.Text = "Pick 1:";

            lbltwo.Location = new Point(258, 163);
            lbltwo.Size = lblSize;
            lbltwo.Font = new Font("Times New Roman", 10.8f, FontStyle.Regular);
            lbltwo.Text = "Pick 2:";

            lblthree.Location = new Point(258, 213);
            lblthree.Size = lblSize;
            lblthree.Font = new Font("Times New Roman", 10.8f, FontStyle.Regular);
            lblthree.Text = "Pick 3:";

            lblfour.Location = new Point(258, 263);
            lblfour.Size = lblSize;
            lblfour.Font = new Font("Times New Roman", 10.8f, FontStyle.Regular);
            lblfour.Text = "Pick 4:";

            lblfive.Location = new Point(258, 313);
            lblfive.Size = lblSize;
            lblfive.Font = new Font("Times New Roman", 10.8f, FontStyle.Regular);
            lblfive.Text = "Pick 5:";

            //display labels
            lblpickone.Location = new Point(350, 113);
            lblpickone.Size = lblSize;
            lblpickone.Font = new Font("Times New Roman", 10.8f, FontStyle.Regular);

            lblpicktwo.Location = new Point(350, 163);
            lblpicktwo.Size = lblSize;
            lblpicktwo.Font = new Font("Times New Roman", 10.8f, FontStyle.Regular);

            lblpickthree.Location = new Point(350, 213);
            lblpickthree.Size = lblSize;
            lblpickthree.Font = new Font("Times New Roman", 10.8f, FontStyle.Regular);

            lblpickfour.Location = new Point(350, 263);
            lblpickfour.Size = lblSize;
            lblpickfour.Font = new Font("Times New Roman", 10.8f, FontStyle.Regular);

            lblpickfive.Location = new Point(350, 313);
            lblpickfive.Size = lblSize;
            lblpickfive.Font = new Font("Times New Roman", 10.8f, FontStyle.Regular);

            //radio button size generic
            Size rsize = new Size(120, 26);

            //radio button properties
            rMon.Location = new Point(252, 403);
            rMon.Size = rsize;
            rMon.Font = new Font("Times New Roman", 10.8f, FontStyle.Regular);
            rMon.Text = "Monday";

            rTue.Location = new Point(373, 403);
            rTue.Size = rsize;
            rTue.Font = new Font("Times New Roman", 10.8f, FontStyle.Regular);
            rTue.Text = "Tuesday";

            rWed.Location = new Point(494, 403);
            rWed.Size = rsize;
            rWed.Font = new Font("Times New Roman", 10.8f, FontStyle.Regular);
            rWed.Text = "Wednesday";

            rThu.Location = new Point(252, 469);
            rThu.Size = rsize;
            rThu.Font = new Font("Times New Roman", 10.8f, FontStyle.Regular);
            rThu.Text = "Thursday";

            rFri.Location = new Point(373, 469);
            rFri.Size = rsize;
            rFri.Font = new Font("Times New Roman", 10.8f, FontStyle.Regular);
            rFri.Text = "Friday";

            rSat.Location = new Point(494, 469);
            rSat.Size = rsize;
            rSat.Font = new Font("Times New Roman", 10.8f, FontStyle.Regular);
            rSat.Text = "Saturday";

            rSun.Location = new Point(373, 535);
            rSun.Size = rsize;
            rSun.Font = new Font("Times New Roman", 10.8f, FontStyle.Regular);
            rSun.Text = "Sunday";
            #endregion

            #region events
            //register event handles
            bgen.Click += new EventHandler(bgen_Click);
            bclear.Click += new EventHandler(bclear_Click);
            #endregion

            #region numobjs
 
            for (int i = 1; i <= 39; i++)
            {
                allnumobjs.Add(new Number()
                {
                    A_Val = i,
                    Is_Even = (i % 2 == 0),
                    Is_Odd = (i % 2 !=0)
                });
            }
            #endregion

            #region readintext

            //reading in the file
            int counter = 0;
            string line;

            //list to add the results to
            List<string> ipicks = new List<string>();

            //read in results
            StreamReader sr = new StreamReader(@"C:\Users\Technology\Desktop\cashfivelotto.txt");

            //iterate through the text file, until we reach the end
            while ((line = sr.ReadLine()) != null)
            {
                //remove the comma's from the file and store the numbers in numset 
                char[] delimiters = new char[] { ',' };
                string[] numset = line.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);

                for (int i = 0; i < numset.Length; i++)
                {
                    //counter for line numbers and formatting 
                    counter++;

                    if (counter % 6 == 0)
                    {
                        //add the text 
                        tbmain.Text += line + System.Environment.NewLine;

                        //enumerate values for each number
                        //based on netvalues and day values
                        for (int j = 1; j < numset.Length; j++)
                        {
                            foreach(Number n in allnumobjs)
                            {
                                if(n.A_Val == int.Parse(numset[j]))
                                {
                                    n.Net_Val += 1;

                                    if (numset[0] == "MON")
                                    {
                                        n.Monday += 1;
                                    }
                                    else if (numset[0] == "TUE")
                                    {
                                        n.Tuesday += 1;
                                    }
                                    else if (numset[0] == "WED")
                                    {
                                        n.Wednesday += 1;
                                    }
                                    else if (numset[0] == "THU")
                                    {
                                        n.Thursday += 1;
                                    }
                                    else if (numset[0] == "FRI")
                                    {
                                        n.Friday += 1;
                                    }
                                    else if (numset[0] == "SAT")
                                    {
                                        n.Saturday += 1;
                                    }
                                    else
                                    {
                                        n.Sunday += 1;
                                    }
                                }
                            }
                        }
                    }
                    ipicks.Add(numset[i]);
                }
                //sr.Close();
            }
            //close streamreader
            sr.Close();
            #endregion

            #region lists

            int average = 0;

            foreach (Number n in allnumobjs)
            {
                average += n.A_Val;
                if (n.Net_Val >= 23)
                {
                    hotnums.Add(n.A_Val);
                }
                else if (n.Net_Val <= 22)
                {
                    coldnums.Add(n.A_Val);
                }

            }

            average = average / allnumobjs.Count;

            //eliminate doubles from the lists
            foreach (int i in coldnums)
            {
                for (int x = 0; x < hotnums.Count; x++)
                {
                    if (i == hotnums[x])
                    {
                        hotnums.Remove(x);
                    }
                }
            }

           // lblpickone.Text = hotnums.Count.ToString();
           // lblpickfive.Text = coldnums.Count.ToString();
     
            #endregion

            #region addcontrols
            //add the controls to the form
            Controls.Add(tbmain);

            Controls.Add(bgen);
            Controls.Add(bclear);

            Controls.Add(lblone);
            Controls.Add(lbltwo);
            Controls.Add(lblthree);
            Controls.Add(lblfour);
            Controls.Add(lblfive);

            Controls.Add(lblpickone);
            Controls.Add(lblpicktwo);
            Controls.Add(lblpickthree);
            Controls.Add(lblpickfour);
            Controls.Add(lblpickfive);

            Controls.Add(rMon);
            Controls.Add(rTue);
            Controls.Add(rWed);
            Controls.Add(rThu);
            Controls.Add(rFri);
            Controls.Add(rSat);
            Controls.Add(rSun);

            #endregion

            //InitializeComponent();
        }

        private void Calculate()
        {
            #region movecalculation
            //day lists for common numbers for the day
            List<int> mon = new List<int>();
            List<int> tue = new List<int>();
            List<int> wed = new List<int>();
            List<int> thu = new List<int>();
            List<int> fri = new List<int>();
            List<int> sat = new List<int>();
            List<int> sun = new List<int>();

            bool check = true;

            int pick1 = 0, pick2 = 0, pick3 = 0, pick4 = 0, pick5 = 0;

            while (check == true)
            {
                if (rMon.Checked)
                {
                    foreach (Number n in this.allnumobjs)
                    {
                        if (n.Monday > 3)
                        {
                            mon.Add(n.A_Val);
                        }
                    }

                    foreach (int i in mon)
                    {
                        if (hotnums.Contains(i))
                        {
                            hotnums.Remove(i);
                        }
                        if (coldnums.Contains(i))
                        {
                            coldnums.Remove(i);
                        }
                    }

                    Random r = new Random();

                    pick1 = hotnums[r.Next(0, hotnums.Count)];

                    pick2 = hotnums[r.Next(0, hotnums.Count)];

                    if (pick1 == pick2)
                    {
                        check = false;
                    }

                    pick3 = mon[r.Next(0, mon.Count)];

                    pick4 = coldnums[r.Next(0, coldnums.Count)];

                    pick5 = coldnums[r.Next(0, coldnums.Count)];

                    if (pick4 == pick5)
                    {
                        check = false;
                    }


                    int picktot = pick1 + pick2 + pick3 + pick4 + pick5;

                    if ((picktot <= 80) || (picktot >= 125))
                    {
                        check = false;
                    }

                    allpicks.Add(pick1);
                    allpicks.Add(pick2);
                    allpicks.Add(pick3);
                    allpicks.Add(pick4);
                    allpicks.Add(pick5);

                    allpicks.Sort();

                    int even = 0;
                    int odd = 0;

                    foreach (int i in allpicks)
                    {
                        foreach (Number n in allnumobjs)
                        {
                            if (i == n.A_Val)
                            {
                                if (n.Is_Even)
                                {
                                    even++;
                                }
                                else
                                {
                                    odd++;
                                }
                            }
                        }
                    }

                    //make sure we keep 4/1, 3/2 split on odds and evens
                    if ((even > 4) || (odd > 4))
                    {
                        check = false;
                    }
                    //first pick shouldnt be more than 14
                    if (allpicks[0] >= 14)
                    {
                        check = false;
                    }

                    //the picks are good, display them
                    lblpickone.Text = allpicks[0].ToString();
                    lblpicktwo.Text = allpicks[1].ToString();
                    lblpickthree.Text = allpicks[2].ToString();
                    lblpickfour.Text = allpicks[3].ToString();
                    lblpickfive.Text = allpicks[4].ToString();

                    allpicks.Clear();
                    mon.Clear();
                }
                else if (rTue.Checked)
                {
                    foreach (Number n in this.allnumobjs)
                    {
                        if (n.Tuesday > 3)
                        {
                            tue.Add(n.A_Val);
                        }
                    }

                    foreach (int i in tue)
                    {
                        if (hotnums.Contains(i))
                        {
                            hotnums.Remove(i);
                        }
                        if (coldnums.Contains(i))
                        {
                            coldnums.Remove(i);
                        }
                    }

                    Random r = new Random();

                    pick1 = hotnums[r.Next(0, hotnums.Count)];

                    pick2 = hotnums[r.Next(0, hotnums.Count)];

                    if (pick1 == pick2)
                    {
                        check = false;
                    }

                    pick3 = tue[r.Next(0, tue.Count)];

                    pick4 = coldnums[r.Next(0, coldnums.Count)];

                    pick5 = coldnums[r.Next(0, coldnums.Count)];

                    if (pick4 == pick5)
                    {
                        check = false;
                    }

                    int picktot = pick1 + pick2 + pick3 + pick4 + pick5;

                    if ((picktot <= 80) || (picktot >= 125))
                    {
                        check = false;
                    }

                    allpicks.Add(pick1);
                    allpicks.Add(pick2);
                    allpicks.Add(pick3);
                    allpicks.Add(pick4);
                    allpicks.Add(pick5);

                    allpicks.Sort();

                    int even = 0;
                    int odd = 0;

                    foreach (int i in allpicks)
                    {
                        foreach (Number n in allnumobjs)
                        {
                            if (i == n.A_Val)
                            {
                                if (n.Is_Even)
                                {
                                    even++;
                                }
                                else
                                {
                                    odd++;
                                }
                            }
                        }
                    }

                    //make sure we keep 4/1, 3/2 split on odds and evens
                    if ((even > 4) || (odd > 4))
                    {
                        check = false;
                    }
                    //first pick shouldnt be more than 14
                    if (allpicks[0] >= 14)
                    {
                        check = false;
                    }

                    //the picks are good, display them
                    lblpickone.Text = allpicks[0].ToString();
                    lblpicktwo.Text = allpicks[1].ToString();
                    lblpickthree.Text = allpicks[2].ToString();
                    lblpickfour.Text = allpicks[3].ToString();
                    lblpickfive.Text = allpicks[4].ToString();

                    allpicks.Clear();
                    tue.Clear();
                }
                else if (rWed.Checked)
                {
                    foreach (Number n in this.allnumobjs)
                    {
                        if (n.Wednesday > 3)
                        {
                            wed.Add(n.A_Val);
                        }
                    }

                    foreach (int i in wed)
                    {
                        if (hotnums.Contains(i))
                        {
                            hotnums.Remove(i);
                        }
                        if (coldnums.Contains(i))
                        {
                            coldnums.Remove(i);
                        }
                    }

                    Random r = new Random();

                    pick1 = hotnums[r.Next(0, hotnums.Count)];

                    pick2 = hotnums[r.Next(0, hotnums.Count)];

                    if (pick1 == pick2)
                    {
                        check = false;
                    }

                    pick3 = wed[r.Next(0, wed.Count)];

                    pick4 = coldnums[r.Next(0, coldnums.Count)];

                    pick5 = coldnums[r.Next(0, coldnums.Count)];

                    if (pick4 == pick5)
                    {
                        check = false;
                    }

                    int picktot = pick1 + pick2 + pick3 + pick4 + pick5;

                    if ((picktot <= 80) || (picktot >= 125))
                    {
                        check = false;
                    }

                    allpicks.Add(pick1);
                    allpicks.Add(pick2);
                    allpicks.Add(pick3);
                    allpicks.Add(pick4);
                    allpicks.Add(pick5);

                    allpicks.Sort();

                    int even = 0;
                    int odd = 0;

                    foreach (int i in allpicks)
                    {
                        foreach (Number n in allnumobjs)
                        {
                            if (i == n.A_Val)
                            {
                                if (n.Is_Even)
                                {
                                    even++;
                                }
                                else
                                {
                                    odd++;
                                }
                            }
                        }
                    }

                    //make sure we keep 4/1, 3/2 split on odds and evens
                    if ((even > 4) || (odd > 4))
                    {
                        check = false;
                    }
                    //first pick shouldnt be more than 14
                    if (allpicks[0] >= 14)
                    {
                        check = false;
                    }

                    //the picks are good, display them
                    lblpickone.Text = allpicks[0].ToString();
                    lblpicktwo.Text = allpicks[1].ToString();
                    lblpickthree.Text = allpicks[2].ToString();
                    lblpickfour.Text = allpicks[3].ToString();
                    lblpickfive.Text = allpicks[4].ToString();

                    allpicks.Clear();
                    wed.Clear();
                }
                else if (rThu.Checked)
                {
                    foreach (Number n in this.allnumobjs)
                    {
                        if (n.Thursday > 3)
                        {
                            thu.Add(n.A_Val);
                        }
                    }

                    foreach (int i in thu)
                    {
                        if (hotnums.Contains(i))
                        {
                            hotnums.Remove(i);
                        }
                        if (coldnums.Contains(i))
                        {
                            coldnums.Remove(i);
                        }
                    }

                    Random r = new Random();

                    pick1 = hotnums[r.Next(0, hotnums.Count)];

                    pick2 = hotnums[r.Next(0, hotnums.Count)];

                    if (pick1 == pick2)
                    {
                        check = false;
                    }

                    pick3 = thu[r.Next(0, thu.Count)];

                    pick4 = coldnums[r.Next(0, coldnums.Count)];

                    pick5 = coldnums[r.Next(0, coldnums.Count)];

                    if (pick4 == pick5)
                    {
                        check = false;
                    }


                    int picktot = pick1 + pick2 + pick3 + pick4 + pick5;

                    if ((picktot <= 80) || (picktot >= 125))
                    {
                        check = false;
                    }

                    allpicks.Add(pick1);
                    allpicks.Add(pick2);
                    allpicks.Add(pick3);
                    allpicks.Add(pick4);
                    allpicks.Add(pick5);

                    allpicks.Sort();

                    int even = 0;
                    int odd = 0;

                    foreach (int i in allpicks)
                    {
                        foreach (Number n in allnumobjs)
                        {
                            if (i == n.A_Val)
                            {
                                if (n.Is_Even)
                                {
                                    even++;
                                }
                                else
                                {
                                    odd++;
                                }
                            }
                        }
                    }

                    //make sure we keep 4/1, 3/2 split on odds and evens
                    if ((even > 4) || (odd > 4))
                    {
                        check = false;
                    }
                    //first pick shouldnt be more than 14
                    if (allpicks[0] >= 14)
                    {
                        check = false;
                    }

                    //the picks are good, display them
                    lblpickone.Text = allpicks[0].ToString();
                    lblpicktwo.Text = allpicks[1].ToString();
                    lblpickthree.Text = allpicks[2].ToString();
                    lblpickfour.Text = allpicks[3].ToString();
                    lblpickfive.Text = allpicks[4].ToString();

                    allpicks.Clear();
                    thu.Clear();
                }
                else if (rFri.Checked)
                {
                    foreach (Number n in this.allnumobjs)
                    {
                        if (n.Friday > 3)
                        {
                            fri.Add(n.A_Val);
                        }
                    }

                    foreach (int i in fri)
                    {
                        if (hotnums.Contains(i))
                        {
                            hotnums.Remove(i);
                        }
                        if (coldnums.Contains(i))
                        {
                            coldnums.Remove(i);
                        }
                    }

                    Random r = new Random();

                    pick1 = hotnums[r.Next(0, hotnums.Count)];

                    pick2 = hotnums[r.Next(0, hotnums.Count)];

                    if (pick1 == pick2)
                    {
                        check = false;
                    }

                    pick3 = fri[r.Next(0, fri.Count)];

                    pick4 = coldnums[r.Next(0, coldnums.Count)];

                    pick5 = coldnums[r.Next(0, coldnums.Count)];

                    if (pick4 == pick5)
                    {
                        check = false;
                    }

                    int picktot = pick1 + pick2 + pick3 + pick4 + pick5;

                    if ((picktot <= 80) || (picktot >= 125))
                    {
                        check = false;
                    }

                    allpicks.Add(pick1);
                    allpicks.Add(pick2);
                    allpicks.Add(pick3);
                    allpicks.Add(pick4);
                    allpicks.Add(pick5);

                    allpicks.Sort();

                    int even = 0;
                    int odd = 0;

                    foreach (int i in allpicks)
                    {
                        foreach (Number n in allnumobjs)
                        {
                            if (i == n.A_Val)
                            {
                                if (n.Is_Even)
                                {
                                    even++;
                                }
                                else
                                {
                                    odd++;
                                }
                            }
                        }
                    }

                    //make sure we keep 4/1, 3/2 split on odds and evens
                    if ((even > 4) || (odd > 4))
                    {
                        check = false;
                    }
                    //first pick shouldnt be more than 14
                    if (allpicks[0] >= 14)
                    {
                        check = false;
                    }

                    //the picks are good, display them
                    lblpickone.Text = allpicks[0].ToString();
                    lblpicktwo.Text = allpicks[1].ToString();
                    lblpickthree.Text = allpicks[2].ToString();
                    lblpickfour.Text = allpicks[3].ToString();
                    lblpickfive.Text = allpicks[4].ToString();

                    allpicks.Clear();
                    fri.Clear();
                }
                else if (rSat.Checked)
                {
                    foreach (Number n in this.allnumobjs)
                    {
                        if (n.Saturday > 3)
                        {
                            sat.Add(n.A_Val);
                        }
                    }

                    foreach (int i in sat)
                    {
                        if (hotnums.Contains(i))
                        {
                            hotnums.Remove(i);
                        }
                        if (coldnums.Contains(i))
                        {
                            coldnums.Remove(i);
                        }
                    }

                    Random r = new Random();

                    pick1 = hotnums[r.Next(0, hotnums.Count)];

                    pick2 = hotnums[r.Next(0, hotnums.Count)];

                    if (pick1 == pick2)
                    {
                        check = false;
                    }

                    pick3 = sat[r.Next(0, sat.Count)];

                    pick4 = coldnums[r.Next(0, coldnums.Count)];

                    pick5 = coldnums[r.Next(0, coldnums.Count)];

                    if (pick4 == pick5)
                    {
                        check = false;
                    }

                    int picktot = pick1 + pick2 + pick3 + pick4 + pick5;

                    if ((picktot <= 80) || (picktot >= 125))
                    {
                        check = false;
                    }

                    allpicks.Add(pick1);
                    allpicks.Add(pick2);
                    allpicks.Add(pick3);
                    allpicks.Add(pick4);
                    allpicks.Add(pick5);

                    allpicks.Sort();

                    int even = 0;
                    int odd = 0;

                    foreach (int i in allpicks)
                    {
                        foreach (Number n in allnumobjs)
                        {
                            if (i == n.A_Val)
                            {
                                if (n.Is_Even)
                                {
                                    even++;
                                }
                                else
                                {
                                    odd++;
                                }
                            }
                        }
                    }

                    //make sure we keep 4/1, 3/2 split on odds and evens
                    if ((even > 4) || (odd > 4))
                    {
                        check = false;
                    }
                    //first pick shouldnt be more than 14
                    if (allpicks[0] >= 14)
                    {
                        check = false;
                    }

                    //the picks are good, display them
                    lblpickone.Text = allpicks[0].ToString();
                    lblpicktwo.Text = allpicks[1].ToString();
                    lblpickthree.Text = allpicks[2].ToString();
                    lblpickfour.Text = allpicks[3].ToString();
                    lblpickfive.Text = allpicks[4].ToString();

                    allpicks.Clear();
                    sat.Clear();
                }
                else if (rSun.Checked)
                {
                    foreach (Number n in this.allnumobjs)
                    {
                        if (n.Sunday > 3)
                        {
                            sun.Add(n.A_Val);
                        }
                    }

                    foreach (int i in sun)
                    {
                        if (hotnums.Contains(i))
                        {
                            hotnums.Remove(i);
                        }
                        if (coldnums.Contains(i))
                        {
                            coldnums.Remove(i);
                        }
                    }
                 
                    Random r = new Random();

                    pick1 = hotnums[r.Next(0, hotnums.Count)];

                    pick2 = hotnums[r.Next(0, hotnums.Count)];

                    if (pick1 == pick2)
                    {
                        check = false;
                    }

                    pick3 = sun[r.Next(0, sun.Count)];

                    pick4 = coldnums[r.Next(0, coldnums.Count)];

                    pick5 = coldnums[r.Next(0, coldnums.Count)];

                    if (pick4 == pick5)
                    {
                        check = false;
                    }

                    int picktot = pick1 + pick2 + pick3 + pick4 + pick5;

                    if ((picktot <= 80) || (picktot >= 125))
                    {
                        check = false;
                    }

                    allpicks.Add(pick1);
                    allpicks.Add(pick2);
                    allpicks.Add(pick3);
                    allpicks.Add(pick4);
                    allpicks.Add(pick5);

                    allpicks.Sort();

                    int even = 0;
                    int odd = 0;

                    foreach (int i in allpicks)
                    {
                        foreach (Number n in allnumobjs)
                        {
                            if (i == n.A_Val)
                            {
                                if (n.Is_Even)
                                {
                                    even++;
                                }
                                else
                                {
                                    odd++;
                                }
                            }
                        }
                    }

                    //make sure we keep 4/1, 3/2 split on odds and evens
                    if ((even > 4) || (odd > 4))
                    {
                        check = false;
                    }
                    //first pick shouldnt be more than 14
                    if (allpicks[0] >= 14)
                    {
                        check = false;
                    }

                    //the picks are good, display them
                    lblpickone.Text = allpicks[0].ToString();
                    lblpicktwo.Text = allpicks[1].ToString();
                    lblpickthree.Text = allpicks[2].ToString();
                    lblpickfour.Text = allpicks[3].ToString();
                    lblpickfive.Text = allpicks[4].ToString();

                    allpicks.Clear();
                    sun.Clear();
                }
                else
                {
                    MessageBox.Show("Please select a day");
                }
            }

            #endregion
        }

        private void bgen_Click(object sender, EventArgs e)
        {
            Calculate();
        }
        private void bclear_Click(object sender, EventArgs e)
        {
            lblpickone.Text = "";
            lblpicktwo.Text = "";
            lblpickthree.Text = "";
            lblpickfour.Text = "";
            lblpickfive.Text = "";
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }

    public class Number
    {
        public Number()
        {
        }

        #region properties

        public int Monday
        {
            get { return this.monday; }
            set
            {
                this.monday = value;
            }
        }
        private int monday;

        public int Tuesday
        {
            get { return this.tuesday; }
            set
            {
                this.tuesday = value;
            }
        }
        private int tuesday;
        

        public int Wednesday
        {
            get { return this.wednesday; }
            set
            {
                this.wednesday = value;
            }
        }
        private int wednesday;

        public int Thursday
        {
            get { return this.thursday; }
            set
            {
                this.thursday = value;
            }
        }
        private int thursday;

        public int Friday
        {
            get { return this.friday; }
            set
            {
                this.friday = value;
            }
        }
        private int friday;

        public int Saturday
        {
            get { return this.saturday; }
            set
            {
                this.saturday = value;
            }
        }
        private int saturday;

        public int Sunday
        {
            get { return this.sunday; }
            set
            {
                this.sunday = value;
            }
        }
        private int sunday;

        public bool Is_Even
        {
            get { return this.is_even; }

            set
            {
                this.is_even = value;
            }
        }
        private bool is_even;

        public bool Is_Odd
        {
            get { return this.is_odd; }

            set
            {
                this.is_odd = value;
            }
        }
        private bool is_odd;

        public bool Is_Hot
        {
            get { return this.is_hot; }

            set
            {
                this.is_hot = value;
            }
        }
        private bool is_hot;

        public bool Is_Cold
        {
            get { return this.is_cold; }

            set
            {
                this.is_cold = value;
            }
        }
        private bool is_cold;

        public int A_Val
        {
            get { return this.a_val; }

            set
            {
                this.a_val = value;
            }
        }
        private int a_val;

        public int Net_Val
        {
            get { return this.net_val; }

            set
            {
                this.net_val = value;
            }
        }
        private int net_val;

        #endregion
    }
}



Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3461
  • View blog
  • Posts: 10,669
  • Joined: 05-May 12

Re: Solving the lottery with C#, this should be fun!

Posted 27 September 2012 - 12:03 AM

Why aren't you using a function to handle the duplicate code for each of the sections within:
if (rDAY.Checked)
{
    // common code that only varies by which day list is used
    // and which day count is used
}



You can probably simplify your code some more if you changed you data structure so that you have a Day enum, and used it as an indexer.
http://msdn.microsof...y/6x16t2tx.aspx
Was This Post Helpful? 0
  • +
  • -

#7 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,957
  • Joined: 21-March 08

Re: Solving the lottery with C#, this should be fun!

Posted 28 September 2012 - 06:54 PM

View PostGleave, on 26 September 2012 - 12:07 PM, said:

Thank you for your responses. Wow I didn't know you could instantiate a new number from a method call that is awesome.


That's not a method. That's the Number constructor, followed by an object initializer.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1