Exemplo n.º 1
0
        private void Awake()
        {
            List <int> list = new List <int>()
            {
                8, 3, -2, 39, 1290, 199, -29, 55, 1, 0
            };
            Pcg randomizer = new Pcg();

            list.Shuffle(randomizer);
            StringBuilder builder = new StringBuilder();

            builder.Append("Output:");
            foreach (int item in list)
            {
                builder.Append('\n').Append(item.ToString());
            }
            Debug.Log(builder.ToString());

            /* Should always be in this order after the shuffle *
            * -29, 3, -2, 199, 55, 8, 0, 1, 39, 1290           */

            Assert.AreEqual(-29, list[0]);
            Assert.AreEqual(3, list[1]);
            Assert.AreEqual(-2, list[2]);
            Assert.AreEqual(199, list[3]);
            Assert.AreEqual(55, list[4]);
            Assert.AreEqual(8, list[5]);
            Assert.AreEqual(0, list[6]);
            Assert.AreEqual(1, list[7]);
            Assert.AreEqual(39, list[8]);
            Assert.AreEqual(1290, list[9]);
        }
Exemplo n.º 2
0
 public static T Choose <T>(this T[] array, Pcg randomizer)
 {
     if (randomizer == null)
     {
         throw new ArgumentNullException(nameof(randomizer));
     }
     if (array.Length == 0)
     {
         throw new InvalidOperationException("Given Array has a length of zero, so no element can be returned.");
     }
     return(array[Convert.ToInt32(randomizer.Range32(Convert.ToUInt32(array.Length)))]);
 }
Exemplo n.º 3
0
 public static T Choose <T>(this IList <T> list, Pcg randomizer)
 {
     if (randomizer == null)
     {
         throw new ArgumentNullException(nameof(randomizer));
     }
     if (list.Count == 0)
     {
         throw new InvalidOperationException("Given IList contains no elements, so no element can be returned.");
     }
     return(list[Convert.ToInt32(randomizer.Range32(Convert.ToUInt32(list.Count)))]);
 }
Exemplo n.º 4
0
        public static T Take <T>(this IList <T> list, Pcg randomizer)
        {
            if (randomizer == null)
            {
                throw new ArgumentNullException(nameof(randomizer));
            }
            if (list.Count == 0)
            {
                throw new InvalidOperationException("Given IList contains no elements, so no element can be returned.");
            }
            T item = Choose(list, randomizer);

            list.Remove(item);
            return(item);
        }
Exemplo n.º 5
0
        public static void Shuffle <T>(this T[] array, Pcg randomizer)
        {
            if (randomizer == null)
            {
                throw new ArgumentNullException(nameof(randomizer));
            }
            int n = array.Length;

            while (n > 0)
            {
                int rand = Convert.ToInt32(randomizer.Range32(Convert.ToUInt32(n--)));
                T   item = array[rand];
                array[rand] = array[n];
                array[n]    = item;
            }
        }
Exemplo n.º 6
0
        public static void Shuffle <T>(this IList <T> list, Pcg randomizer)
        {
            if (randomizer == null)
            {
                throw new ArgumentNullException(nameof(randomizer));
            }
            int n = list.Count;

            while (n > 0)
            {
                int rand = Convert.ToInt32(randomizer.Range32(Convert.ToUInt32(n--)));
                T   item = list[rand];
                list[rand] = list[n];
                list[n]    = item;
            }
        }