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);
}
}