// Primitive Power Ball C# Samples Generator - Global Lotomania in action:)
// David Taran January 11, 2016
// For PowerBall as a answer type: 5, 69, 1, 26, -1, 10... 

using System;

namespace Loto5p1
{
    class Program
    {
        static void Main(string[] args)
        {
            int iRegularBallsCount,
                iRegularBallsMaxNumber,
                iPowerBallsCount,
                iPowerBallsMaxNumber,
                iGenerationInitialNumber,
                iDrawingsNumber;
            do
            {
                Console.Write("Ener Regular Balls Count: ");
                iRegularBallsCount = Int32.Parse(Console.ReadLine());
            }
            while (0 > iRegularBallsCount);

            do
            {
                Console.Write("Ener Regular Balls Max Number: ");
                iRegularBallsMaxNumber = Int32.Parse(Console.ReadLine());
            }
            while (iRegularBallsCount >= iRegularBallsMaxNumber);

            do
            {
                Console.Write("Ener Power Balls Count: ");
                iPowerBallsCount = Int32.Parse(Console.ReadLine());
            }
            while (0 > iPowerBallsCount);

            do
            {
                Console.Write("Ener Power Balls Max Number: ");
                iPowerBallsMaxNumber = Int32.Parse(Console.ReadLine());
            }
            while (iPowerBallsCount >= iPowerBallsMaxNumber);

                Console.Write("Ener Your Generation Initial Number (-1 for std gen): ");
                iGenerationInitialNumber = Int32.Parse(Console.ReadLine());

                Console.Write("Ener Drawings Number: ");
                iDrawingsNumber = Int32.Parse(Console.ReadLine());

            Loto loto = new Loto(iRegularBallsCount, iRegularBallsMaxNumber,
                                 iPowerBallsCount, iPowerBallsMaxNumber,
                                 iGenerationInitialNumber);

            for (int i = 0; i < iDrawingsNumber; i++)
            {
                Console.WriteLine("\n"+loto.GetOneDrawing());
            }
            Console.ReadLine();
        }
    }

    class Loto
    {
        private
            int iRegularSamplesCount,
                iRegularBallsCount,
                iPowerSamplesCount,
                iPowerBallsCount,
                iGenerationInitialNumber;
            RandomSample    rdsBall = new RandomSample(),
                            rdsPower= new RandomSample();
            int[] iRegular;
            int[] iPower;

        public
        Loto(int iRegularSamplesCount_, int iRegularBallsCount_,
             int iPowerSamplesCount_,   int iPowerBallsCount_,
             int iGenerationInitialNumber_)
        {
            iRegularSamplesCount    = iRegularSamplesCount_;
            iRegularBallsCount      = iRegularBallsCount_;
            iPowerSamplesCount      = iPowerSamplesCount_;
            iPowerBallsCount        = iPowerBallsCount_;
            iGenerationInitialNumber = iGenerationInitialNumber_;

            iRegular = new int[iRegularSamplesCount];
            iPower = new int[iPowerSamplesCount];

            rdsBall.Init(iRegularSamplesCount, iRegularBallsCount, iGenerationInitialNumber);
            rdsPower.Init(iPowerSamplesCount, iPowerBallsCount, iGenerationInitialNumber);
        }

        public
        string GetOneDrawing()
        {
            rdsBall.GetSamplesSet(iRegular);
            rdsPower.GetSamplesSet(iPower);
            string str = "";
            foreach (int i in iRegular)
                str += String.Format(" {0:d2}",i);
            foreach (int i in iPower)
                str += String.Format("  {0:d2}", i);
            return str;
        }
    }

    class RandomSample
    {
        private
            int iSamplesCount,
                iMaxSampleValue;
            bool bIsInit;
            Random rnd;
            double[] rnd2intDelta;
            int[] iAllValidNumbers;
            int[] iSamplesSet;

        public RandomSample()
        {
            bIsInit = false;
        }
        public
        void Init(int iSamplesCount_, int iMaxSampleValue_, int iRndInitValue)
        {
            if (0 > iSamplesCount_  || 0 > iMaxSampleValue_ 
                                    || iSamplesCount_ >= iMaxSampleValue_)
                return;

            iSamplesCount   = iSamplesCount_;
            iMaxSampleValue = iMaxSampleValue_;

            if (-1 == iRndInitValue)
                rnd = new Random();
            else
                rnd = new Random(iRndInitValue);

            rnd2intDelta = new double[iSamplesCount];

            int i, k;
            for (i=0, k=iMaxSampleValue; i<iSamplesCount; i++,k--)
                rnd2intDelta[i] = 1e0/(double)k;

            iAllValidNumbers = new int[iMaxSampleValue];
            iSamplesSet = new int[iSamplesCount];

            bIsInit = true;
        }
        public
        bool GetSamplesSet(int [] iSample)
        {
            if (!bIsInit)
                return false;

            int i, j, k;
            double drnd;

            for (i = 0; i < iMaxSampleValue; )
                iAllValidNumbers[i] = ++i;

            for (j = 0, k = iMaxSampleValue; j < iSamplesCount; j++,k--)
            {
                drnd = rnd.NextDouble();
                i = (int)(drnd / rnd2intDelta[j]);
                if (i == k)
                    i--;

                iSamplesSet[j] = iAllValidNumbers[i];
                if (i != k-1)
                    iAllValidNumbers[i] = iAllValidNumbers[k-1];
            }

            Array.Sort(iSamplesSet);
            iSamplesSet.CopyTo(iSample, 0);

            return true;
        }
    }
}

Recommended Answers

All 4 Replies

Not sure if you have a problem or if this is supposed to be a snippet. However, one thing I noticed, you are accepting user input without verifying its validity. Using the TryParse method instead of the Parse method would help in this. Something like this:

        do
        {
            Console.Write("Ener Regular Balls Count: ");
        }
        while (!Int32.TryParse(Console.Readline(),out iRegularBallsCount) || 0 > iRegularBallsCount);

This will validate the users input to be only an integer and keep the user inside the loop until it is valid and without throwing an exception.

commented: Nice advice +15

Dear Tinstaafl,
Of course you're right. I suggested the scheme and anyone can improve and modify it, or suggest an alternative approach, in accordance with the vision problem.
Best regards,
David

What exactly is the vision problem? I don't see a mention of it in your original post.

So are you trying to create an application to generate possible random numbers that you could then use to create your tickets? Or are you planning to try and find a pattern in the numbers that have been drawn

(I will tell you if it's the 2nd option, you probably won't have much luck, since the lottery numbers are true random, with no known possible way of finding a pattern)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.