示例#1
0
        public static string GetChance(string[] values, string lastUsed)
        {
            int selected = -1;
            int i        = -1;

            if (!string.IsNullOrEmpty(lastUsed))
            {
                i = Array.FindIndex(values, item => item == lastUsed);
            }

            if (i != -1 && values[i] != lastUsed)
            {
                SharedHelper.LogError("Error locating value '" + lastUsed + "' in array!");
            }

            if (i != -1)
            {
                SharedHelper.Swap(values, i, values.Length - 1); //disable the last used one

                if (values[values.Length - 1] != lastUsed)
                {
                    SharedHelper.LogError("Last value in array is incorrect!");
                }

                selected = rndChances.Next(0, values.Length - 1); //the last one used is excluded from the selection

                if (values[selected] == lastUsed)
                {
                    SharedHelper.LogError("Last used is going to be used again! Not correct!");
                }
            }
            else
            {
                selected = rndChances.Next(0, values.Length);
            }

            return(values[selected]);
        }