NonRepeatingRandom

İstenilen sayıya kadar tekrarlanan random sayıyı etmeyi sağlar.
mail@pastecode.io avatar
unknown
csharp
2 years ago
3.2 kB
5
Indexable
Never
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Tetris_Test.Tetris_Classes
{
    public class NonRepeatingRandom
    {
        private Random random;
        private int[] repeatinglist;
        public int minValue = 0, maxValue = 0, repeatingIndex = 1;
        public readonly int repeatingCount = 0;

        public NonRepeatingRandom(int minValue, int maxValue, int repeatingCount = 2)
        {
            if (minValue == maxValue || minValue + 4 >= maxValue || minValue > maxValue)
            {
                throw new ArgumentException("Min Value must be at least 5 numbers greater than Max Value.");
            }

            if (repeatingCount <= 0) repeatingCount = 1;
            if (repeatingCount > 10) repeatingCount = 10;

            random = new Random();
            repeatinglist = new int[repeatingCount];
            this.repeatingCount = repeatingCount;
            this.minValue = minValue;
            this.maxValue = maxValue;
        }

        public int Next()
        {
            int number = 0;

            while (true)
            {
                number = random.Next(minValue, maxValue);

                if (!IsNumberRepeating(number))
                    break;
            }

            SwapAndAddArray(number);
            return number;
        }

        private bool IsNumberRepeating(int number)
        {
            bool listCheck = false;
            for (int i = 1; i < repeatinglist.Length; i++)
            {
                if (repeatinglist[i] != repeatinglist[i - 1])
                {
                    listCheck = false;
                    break;
                }
                else
                    listCheck = true;
            }

            if (number == repeatinglist[repeatinglist.Length - 1])
            {
                if (listCheck == true)
                {
                    if (repeatingIndex == repeatingCount)
                    {
                        repeatingIndex = 1;
                        return true;
                    }
                    else
                    {
                        repeatingIndex++;
                        return false;
                    }
                }
                else
                {
                    if (repeatingCount == 1)
                    {
                        repeatingIndex = 1;
                        return true;
                    }
                    else
                    {
                        repeatingIndex++;
                        return false;
                    }
                }
            }
            else
            {
                repeatingIndex = 1;
                return false;
            }
        }

        private void SwapAndAddArray(int number)
        {
            int temp = repeatinglist[repeatinglist.Length - 1];

            for (int i = 0; i < repeatinglist.Length - 1; i++)
            {
                repeatinglist[i] = repeatinglist[i + 1];
            }

            repeatinglist[repeatinglist.Length - 1] = number;
        }
    }
}