Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / February 2008

Tip: Looking for answers? Try searching our database.

Help Needed Initialising Class

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Mick Walker - 17 Feb 2008 19:09 GMT
I have the following class:

class Axis
    {
        private int _num1;
        private int _num2;
        private int _num3;

        public int Num1 {
            get { return _num1; }
            set { _num1 = value; }
        }

        public int Num2 {
            get { return _num2; }
            set { _num2 = value; }
        }

        public int Num3 {
            get { return _num3; }
            set { _num3 = value; }
        }

        public Lotto() {

    }

        public Lotto(int[] ExcludedNumbers) {
        // Initialise variable to random numbers not in the above array
    }
}

What I would like, to be able to do is initialise this class setting
Num1, Num2, Num3 to random values (they must differ from each other).
However when I initialise it, passing an array of integer I would like
Num1, Num2, Num3 to be initialised to random values, but if the value
exists in the ExcludedNumbers array, a different random value should be
used, as before, these values should all differ from each other.

If someone could help that would be great, I'm not nessesarily looking
for a code soloution, but just some help on they way.

Kind Regards
Mick Walker
Marc Gravell - 17 Feb 2008 19:19 GMT
How about something along the lines of below; note that it isn't fully
thread-safe, since I haven't sync'd access to the shared random class:

       public Lotto(int[] ExcludedNumbers)
       {
           Num1 = GetRand(ExcludedNumbers);
           Num2 = GetRand(ExcludedNumbers, Num1);
           Num3 = GetRand(ExcludedNumbers, Num1, Num2);

       }
       public static int GetRand(int[] primaryExclusions, params
int[] secondaryExclusions)
       {
           do // watch out for saturation!!! (i.e. impossible to find
another number)
           {
               int val = rand.Next(); // range...
               if (Array.IndexOf(primaryExclusions, val) < 0
                   && Array.IndexOf(secondaryExclusions, val) < 0)
               {
                   return val;
               }
           } while (true);
       }

       static Random rand = new Random();

Marc

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.