Пример #1
0
        private ShuffleInfo ExtractShuffleInfo(AType left, AType right)
        {
            ShuffleInfo info =
                new ShuffleInfo(ExtractInteger(left), ExtractInteger(right));

            if (info.ItemCount > info.MaxValue)
            {
                throw new Error.Domain(DomainErrorText);
            }

            return(info);
        }
Пример #2
0
        private ShuffleInfo ExtractShuffleInfo(AType left, AType right)
        {
            ShuffleInfo info = 
                new ShuffleInfo(ExtractInteger(left), ExtractInteger(right));

            if (info.ItemCount > info.MaxValue)
            {
                throw new Error.Domain(DomainErrorText);
            }

            return info;
        }
Пример #3
0
        /// <summary>
        /// Shuffle the items with a given seed.
        /// </summary>
        /// <param name="seed"></param>
        /// <param name="info">Information for shuffling.</param>
        /// <remarks>
        /// An implementation of "modified" Durstenfeld's algorithm.
        /// http://en.wikipedia.org/wiki/Fisher-Yates_shuffle
        /// </remarks>
        /// <returns></returns>
        private AType Shuffle(int seed, ShuffleInfo info)
        {
            AType result = AArray.Create(ATypes.AInteger);

            int[]  randomList = Enumerable.Range(0, info.MaxValue).ToArray();
            Random random     = (seed != -1) ? new Random(seed) : new Random();
            int    currentIndex;

            for (int i = info.MaxValue - 1; i >= info.MaxValue - info.ItemCount; i--)
            {
                currentIndex = random.Next(i);
                result.AddWithNoUpdate(AInteger.Create(randomList[currentIndex]));
                randomList[currentIndex] = randomList[i];
            }

            result.Length = info.ItemCount;
            result.Shape  = new List <int>()
            {
                info.ItemCount
            };
            result.Rank = 1;

            return(result);
        }
Пример #4
0
        public override AType Execute(AType right, AType left, Aplus environment)
        {
            ShuffleInfo info = ExtractShuffleInfo(left, right);

            return(Shuffle(GetSeed(environment), info));
        }
Пример #5
0
        /// <summary>
        /// Shuffle the items with a given seed.
        /// </summary>
        /// <param name="seed"></param>
        /// <param name="info">Information for shuffling.</param>
        /// <remarks>
        /// An implementation of "modified" Durstenfeld's algorithm.
        /// http://en.wikipedia.org/wiki/Fisher-Yates_shuffle
        /// </remarks>
        /// <returns></returns>
        private AType Shuffle(int seed, ShuffleInfo info)
        {
            AType result = AArray.Create(ATypes.AInteger);

            int[] randomList = Enumerable.Range(0, info.MaxValue).ToArray();
            Random random = (seed != -1) ? new Random(seed) : new Random();
            int currentIndex;

            for (int i = info.MaxValue - 1; i >= info.MaxValue - info.ItemCount; i--)
            {
                currentIndex = random.Next(i);
                result.AddWithNoUpdate(AInteger.Create(randomList[currentIndex]));
                randomList[currentIndex] = randomList[i];
            }

            result.Length = info.ItemCount;
            result.Shape = new List<int>() { info.ItemCount };
            result.Rank = 1;

            return result;
        }