Example #1
0
        /**
         * @brief 隨機排列陣列元素
         * @param array 陣列物件
         */
        public static void RandomShuffle <T>(T[] array)
        {
            int iCount = array.Length;

            while (iCount > 1)
            {
                iCount--;
                Swap(ref array[iCount], ref array[RandObject.Next(iCount + 1)]);
            }//while
        }
Example #2
0
        /**
         * @brief 隨機排列列表元素
         * @param list 列表物件
         */
        public static void RandomShuffle <T>(List <T> list)
        {
            int iCount = list.Count;
            int iRand  = 0;

            while (iCount > 1)
            {
                iCount--;
                iRand = RandObject.Next(iCount + 1);

                T v = list[iCount];

                list[iCount] = list[iRand];
                list[iRand]  = v;
            }//while
        }
Example #3
0
        /**
         * @brief 隨機取得列表內容
         * @param dir 列表物件
         */
        public static KeyValuePair <K, V> RandomPick <K, V>(Dictionary <K, V> dir)
        {
            int iRand = RandObject.Next(dir.Count);
            int iPos  = 0;

            foreach (KeyValuePair <K, V> Itor in dir)
            {
                if (iPos != iRand)
                {
                    ++iPos;
                }
                else
                {
                    return(Itor);
                }
            }//for

            return(new KeyValuePair <K, V>());
        }
Example #4
0
        /**
         * @brief 隨機取得列表內容
         * @param set 列表物件
         */
        public static T RandomPick <T>(HashSet <T> set)
        {
            int iRand = RandObject.Next(set.Count);
            int iPos  = 0;

            foreach (T Itor in set)
            {
                if (iPos != iRand)
                {
                    ++iPos;
                }
                else
                {
                    return(Itor);
                }
            }//for

            return(default(T));
        }
Example #5
0
 /**
  * @brief 隨機取得列表內容
  * @param list 列表物件
  */
 public static T RandomPick <T>(List <T> list)
 {
     return(list.Count > 0 ? list[RandObject.Next(list.Count)] : default(T));
 }