Issue with selecting an item from a list when has already been removed

Hello (unsure if this is the best place to put this question but anyway…),

I’m fairly new to Unity and C# (so I apologise for any messy or inefficient code) and I am trying to select an item (integer) from a list at random, store the value in its own variable, remove that item from the list and then repeat for 4 times.

In its current state, it works well but I’m still finding that a digit will sometimes be selected from the list even after being removed. I’ve put some ‘Debug.Log’ lines in and they have proven that the values are indeed removed, so my suspicion is towards the ‘random.Next(M_Code_List.Count)’ lines, but again I know little about the language.

I have created this code from reading bits and pieces of the Unity Scripting Reference so I don’t have a complete and utter understanding of what I am writing.

Any assistance would be greatly appreciated and any further clarification can be supplied.

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
...
public static System.Random random = new System.Random();
...

public class Mastermind_Code : MonoBehaviour
{
void Update()
    {       
            var M_Code_List = new List<int> { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
          
            int M_Code0 = random.Next(M_Code_List.Count);
            M_Code_List.Remove(M_Code0);

            // CONTENTS LEFT IN LIST (M_Code0)
            foreach (var x in M_Code_List)
            {
                Debug.Log(x.ToString());
            }
            Debug.Log("===================");
          

            int M_Code1 = random.Next(M_Code_List.Count);
            M_Code_List.Remove(M_Code1);
          
            // CONTENTS LEFT IN LIST (M_Code1)
            foreach (var x in M_Code_List)
            {
                Debug.Log(x.ToString());
            }
            Debug.Log("===================");


            int M_Code2 = random.Next(M_Code_List.Count);
            M_Code_List.Remove(M_Code2);
          
            // CONTENTS LEFT IN LIST (M_Code2)
            foreach (var x in M_Code_List)
            {
                Debug.Log(x.ToString());
            }
            Debug.Log("===================");
          


            int M_Code3 = random.Next(M_Code_List.Count);
            M_Code_List.Remove(M_Code3);
          
          

            // CONTENTS LEFT IN LIST (M_Code3)
            foreach (var x in M_Code_List)
            {
                Debug.Log(x.ToString());
            }
            Debug.Log("===================");
     


            Debug.Log(M_Code0);
            Debug.Log(M_Code1);
            Debug.Log(M_Code2);
            Debug.Log(M_Code3);

            string Mastercode = M_Code0.ToString() + M_Code1.ToString() +                 
            M_Code2.ToString() + M_Code3.ToString();

            Debug.Log(Mastercode);
    }
}

The best place for this is the scripting forum. I’ll move your post there for you.

Seems like it should work, though I use a different technique (Random.Range) so am not sure. Your code can be simplified considerably though, everything put into one for loop. You can easily extend it, like being able to select an arbitrary number of items from an arbitrarily long list, for example. Could be handy for other projects.

The problem is that you aren’t storing the selected numbers from the list, but the selected random index.
Instead of (and at all places… 1, 2, 3 too):

int M_Code0 = random.Next(M_Code_List.Count);

use this:

int selectedIndex = random.Next(M_Code_List.Count);
int M_Code0 = M_Code_List[selectedIndex];
M_Code_List.Remove(selectedIndex);

Do this with all of your selections, I guess you can apply the principle.

Nice catch, I missed that. Doh!