Trying to get a random result without a repeat.

Hello, in the project I am working on I am trying to instantiate these “recruits” off-screen and have them walk into the screen but to a random spot designated by an array of transforms. Therefore, I am trying to make a random number generator that does not repeat numbers, until it hits the max number of transforms I have placed. Once it can no longer give the “recruit” a special number, I want the used number list to clear and start the process over. In my current form of this code, it will overlap numbers without ever clearing the list and I am not really sure why. I am open to the idea that I am approaching this issue the wrong way, and would love any advice or code fixes. Thank you in advance. Here is the relevant code, and I will be watching this thread if you have any questions that might help you help me.

The code here is located on my game controller, independent of the recruits themself.

        public Transform[] recruitMoveTo;
        public List<int> usedRecruitMovePoints = new List<int>();

        public int RecruitMoveTo()
    {
        int MoveTo;
        MoveTo = RollMoveRecruit();
        if (usedRecruitMovePoints.Count == recruitMoveTo.Length)
        {
            Debug.Log("List cleared");
            ClearUsedRecruitList();
        }
        if (usedRecruitMovePoints.Contains(MoveTo))
        {
                while (usedRecruitMovePoints.Contains(MoveTo))
                {
                    MoveTo = RollMoveRecruit();
                }
        }
        else
        {
            usedRecruitMovePoints.Add(MoveTo);
        }
        Debug.Log(MoveTo);
        return MoveTo;
    }

    private int RollMoveRecruit()
    {
        int x;
        x = Random.Range(0, recruitMoveTo.Length);
        return x;
    }

I am calling this function on a script attached to the recruit’s gameobject.

        if (myTeam == teams[2])
        {
            agent.SetDestination(gameController.recruitMoveTo[gameController.RecruitMoveTo()].position);
        }

Thanks again for any help or ideas.

Can’t you shuffle the array, and then loop through it? Otherwise you could tag the positions already selected.

As @Redden44 said, shuffling is probably the easiest solution. Fill a list with the values you want then shuffle it using something like this:

    /// <summary>
    /// Shuffle the array using the Fisher-Yates algorithm
    /// List version
    /// </summary>
    /// <typeparam name="T">Array element type.</typeparam>
    /// <param name="array">Array to shuffle.</param>
    public static void Shuffle<T>( List< T > array )
    {
        int n = array.Count;
        for ( int i = 0 ; i < n ; i++ )
        {
            // Use Next on random instance with an argument.
            // ... The argument is an exclusive bound.
            //     So we will not go past the end of the array.
            int r = i + UnityEngine.Random.Range( 0 , n - i );
            T t = array[ r ];
            array[ r ] = array[ i ];
            array[ i ] = t;
        }
    }