コード例 #1
0
        /// <summary>Fast random will only work if you are sure the dictonary is indexed like an array!</summary>
        public static T GetRandomObjectFromCollection <T>(Dictionary <int, T> thisDictionary, bool fastRandom = false)
        {
            int targetIndex = ThreadedRandom.Range(0, thisDictionary.Count);

            if (fastRandom)
            {
                return(thisDictionary[targetIndex]);
            }

            int currentIndex = 0;

            foreach (var thisPair in thisDictionary)
            {
                if (currentIndex == targetIndex)
                {
                    return(thisPair.Value);
                }
                currentIndex++;
            }

            throw new Exception("This should not happen!");
        }
コード例 #2
0
 public static T GetRandomObjectFromCollection <T>(List <T> thisList) => thisList[ThreadedRandom.Range(0, thisList.Count)];
コード例 #3
0
 public static T GetRandomObjectFromCollection <T>(T[] thisArray) => thisArray[ThreadedRandom.Range(0, thisArray.Length)];