I made this code to randomly select a hue for a given pet. I have two questions regarding this code. I realize that there may be a function for this built in already but I still wanted to try to make my own.

1. I feel like this is far to much code to accomplish what I want and I don't feel like this is the best way to create "random" percentage chances. ( I know there is a random double command and also a random list command. I had issues teaching myself how to use these commands.)

2. I can make this a stand alone function and just call the function rather than including this code in every mobile file correct?

Code:
 //Random Color Selection
            int hueNum = Utility.RandomMinMax(0, 12);
            switch (hueNum) //Selects Color Category
            {
                case 0: //Purple Category
                case 1:
                    int purNum = Utility.RandomMinMax(0, 8);
                    switch (purNum)
                    {
                        case 0:
                        case 1:
                            this.Hue = 0xA33;
                            break;
                        case 2:
                        case 3:
                            this.Hue = 0xA24;
                            break;
                        case 4:
                        case 5:
                            this.Hue = 0xA3C;
                            break;
                        case 6:
                        case 7:
                            this.Hue = 0xA30;
                            break;
                        case 8:
                            this.Hue = 0x77F;
                            break;
                        default:
                            this.Hue = 0x422;
                            break;
                    }
                    break;
                case 2: //Blue Category
                case 3:
                    int bluNum = Utility.RandomMinMax(0, 8);
                    switch (bluNum)
                    {
                        case 0:
                        case 1:
                            this.Hue = 0xA13;
                            break;
                        case 2:
                        case 3:
                            this.Hue = 0x7BB;
                            break;
                        case 4:
                        case 5:
                            this.Hue = 0x4F1;
                            break;
                        case 6:
                        case 7:
                            this.Hue = 0x57;
                            break;
                        case 8:
                            this.Hue = 0x77C;
                            break;
                        default:
                            this.Hue = 0x422;
                            break;
                    }
                    break;
                case 4: //Pink Category
                case 5:
                    int pinNum = Utility.RandomMinMax(0, 8);
                    switch (pinNum)
                    {
                        case 0:
                        case 1:
                            this.Hue = 0x78A;
                            break;
                        case 2:
                        case 3:
                            this.Hue = 0x1C;
                            break;
                        case 4:
                        case 5:
                            this.Hue = 0x80;
                            break;
                        case 6:
                        case 7:
                            this.Hue = 0x78B;
                            break;
                        case 8:
                            this.Hue = 0x503;
                            break;
                        default:
                            this.Hue = 0x422;
                            break;
                    }
                    break;
                case 6: //Yellow Category
                case 7:
                    int yelNum = Utility.RandomMinMax(0, 8);
                    switch (yelNum)
                    {
                        case 0:
                        case 1:
                            this.Hue = 0x4EB;
                            break;
                        case 2:
                        case 3:
                            this.Hue = 0x4EC;
                            break;
                        case 4:
                        case 5:
                            this.Hue = 0x550;
                            break;
                        case 6:
                        case 7:
                            this.Hue = 0xAA1;
                            break;
                        case 8:
                            this.Hue = 0x496;
                            break;
                        default:
                            this.Hue = 0x422;
                            break;
                    }
                    break;
                case 8: //Green Category
                case 9:
                    int grnNum = Utility.RandomMinMax(0, 8);
                    switch (grnNum)
                    {
                        case 0:
                        case 1:
                            this.Hue = 0x5B4;
                            break;
                        case 2:
                        case 3:
                            this.Hue = 0x4E;
                            break;
                        case 4:
                        case 5:
                            this.Hue = 0x110;
                            break;
                        case 6:
                        case 7:
                            this.Hue = 0x506;
                            break;
                        case 8:
                            this.Hue = 0x77B;
                            break;
                        default:
                            this.Hue = 0x422;
                            break;
                    }
                    break;
                case 10: //Red Category
                case 11:
                    int redNum = Utility.RandomMinMax(0, 8);
                    switch (redNum)
                    {
                        case 0:
                        case 1:
                            this.Hue = 0x7AC;
                            break;
                        case 2:
                        case 3:
                            this.Hue = 0x21;
                            break;
                        case 4:
                        case 5:
                            this.Hue = 0xE8;
                            break;
                        case 6:
                        case 7:
                            this.Hue = 0x494;
                            break;
                        case 8:
                            this.Hue = 0x780;
                            break;
                        default:
                            this.Hue = 0x422;
                            break;
                    }
                    break;
                case 12: //2-Tone !!Rare!! Category
                    int rareNum = Utility.RandomMinMax(0, 5);
                    switch (rareNum)
                    {
                        case 0:
                            this.Hue = 0x795;
                            break;
                        case 1:
                            this.Hue = 0x503;
                            break;
                        case 2:
                            this.Hue = 0x508;
                            break;
                        case 3:
                            this.Hue = 0x509;
                            break;
                        case 4:
                            this.Hue = 0xAA9;
                            break;
                        case 5:
                            this.Hue = 0xAA8;
                            break;
                        default:
                            this.Hue = 0x422;
                            break;
                    }
                    break;
                default:
                    this.Hue = 0x422;
                    break;
            }
 
Here is what I did to pick from one of the available hues that are not completely black a few years back.

Code:
Hue = Utility.RandomList(Utility.RandomMinMax(1, 1058), Utility.RandomMinMax(1102, 1177), Utility.RandomMinMax(1190, 1196), Utility.RandomMinMax(1201, 1289), Utility.RandomMinMax(1301, 1378), Utility.RandomMinMax(1401, 1454), Utility.RandomMinMax(1458, 1463), Utility.RandomMinMax(1501, 1554), Utility.RandomMinMax(1601, 1654), Utility.RandomMinMax(1701, 1779), Utility.RandomMinMax(1801, 1908), Utility.RandomMinMax(2001, 2067), Utility.RandomMinMax(2101, 2130), Utility.RandomMinMax(2201, 2224), Utility.RandomMinMax(2301, 2318), Utility.RandomMinMax(2401, 2430), Utility.RandomMinMax(2498, 2644), Utility.RandomMinMax(2651, 2662), Utility.RandomMinMax(2671, 2718), Utility.RandomMinMax(2958, 2970), 2947, 2949, 2951, 2952, 2953, 2955, 2956);
 
This code is so concise :eek:! Any possibility that you can elaborate on what is happening here? If I were to guess what is happening....It seems that you are calling the randomlist and passing another utility as a parameter. After this the min/max numbers you are passing do what? Are these HUE number values? Thank you for your initial response!
 
https://github.com/ServUO/ServUO/blob/master/Server/Utility.cs#L912

Utility provides random hue methods, perhaps they will be useful to you in showing you what's going on too.

This is DMurphy's example extrapolated into it's less concise pattern; it works exactly the same but should be easier to visualize its constituent components;
C#:
int rand1 = Utility.RandomMinMax(1, 1058);
int rand2 = Utility.RandomMinMax(1102, 1177);
int rand3 = Utility.RandomMinMax(1190, 1196);
int rand4 = Utility.RandomMinMax(1201, 1289);
int rand5 = Utility.RandomMinMax(1301, 1378);
int rand6 = Utility.RandomMinMax(1401, 1454);
int rand7 = Utility.RandomMinMax(1458, 1463);
int rand8 = Utility.RandomMinMax(1501, 1554);
int rand9 = Utility.RandomMinMax(1601, 1654);
int rand10 = Utility.RandomMinMax(1701, 1779);
int rand11 = Utility.RandomMinMax(1801, 1908);
int rand12 = Utility.RandomMinMax(2001, 2067);
int rand13 = Utility.RandomMinMax(2101, 2130);
int rand14 = Utility.RandomMinMax(2201, 2224);
int rand15 = Utility.RandomMinMax(2301, 2318);
int rand16 = Utility.RandomMinMax(2401, 2430);
int rand17 = Utility.RandomMinMax(2498, 2644);
int rand18 = Utility.RandomMinMax(2651, 2662);
int rand19 = Utility.RandomMinMax(2671, 2718);
int rand20 = Utility.RandomMinMax(2958, 2970);

RandomList picks a random from all of the supplied numbers;
C#:
Hue = Utility.RandomList(
               rand1,
               rand2,
               rand3,
               rand4,
               rand5,
               rand6,
               rand7,
               rand8,
               rand9,
               rand10,
               rand11,
               rand12,
               rand13,
               rand14,
               rand15,
               rand16,
               rand17,
               rand18,
               rand19,
               rand20,
               // Below are hard-coded single hue numbers
               2947,
               2949,
               2951,
               2952,
               2953,
               2955,
               2956);

In theory, it would actually be more efficient to do this:
C#:
int hue;

switch( Utility.Random( 21 ) ) // Random number 0 to 20 inclusive
{
    case 0: hue = Utility.RandomMinMax(1, 1058); break;
    case 1: hue = Utility.RandomMinMax(1102, 1177); break;
    case 2: hue = Utility.RandomMinMax(1190, 1196); break;
    case 3: hue = Utility.RandomMinMax(1201, 1289); break;
    case 4: hue = Utility.RandomMinMax(1301, 1378); break;
    case 5: hue = Utility.RandomMinMax(1401, 1454); break;
    case 6: hue = Utility.RandomMinMax(1458, 1463); break;
    case 7: hue = Utility.RandomMinMax(1501, 1554); break;
    case 8: hue = Utility.RandomMinMax(1601, 1654); break;
    case 9: hue = Utility.RandomMinMax(1701, 1779); break;
    case 10: hue = Utility.RandomMinMax(1801, 1908); break;
    case 11: hue = Utility.RandomMinMax(2001, 2067); break;
    case 12: hue = Utility.RandomMinMax(2101, 2130); break;
    case 13: hue = Utility.RandomMinMax(2201, 2224); break;
    case 14: hue = Utility.RandomMinMax(2301, 2318); break;
    case 15: hue = Utility.RandomMinMax(2401, 2430); break;
    case 16: hue = Utility.RandomMinMax(2498, 2644); break;
    case 17: hue = Utility.RandomMinMax(2651, 2662); break;
    case 18: hue = Utility.RandomMinMax(2671, 2718); break;
    case 19: hue = Utility.RandomMinMax(2958, 2970); break;
    default: hue = Utility.RandomList(2947, 2949, 2951, 2952, 2953, 2955, 2956); break;
}

Because 1) all possible random values are not pre-generated and 2) an array is only created in the circumstance of case 20 (default), instead of every time.
 
Back