- Home /
Can't add multiple integers to a List in a Loop!
Hey, I've had this problem for a long time now. I think I know what the problem is, but I don't know how to solve it. Here is my problem: I want that every time I call the Run()
function, a random number ( i
) that has not been used before is debugged.
public List List01 = new List();
void Run()
{
while (List01.Contains(i))
{
i = Random.Range(1, 10);
}
List01.Add(i);
//make something with i like debugging
Debug.Log(i);
}
my research says that the reference of i
does not change and so the index is always overwritten. Can anyone help me?
thanks!
This loop is irrelevant because no 'i' values are ever added to the list, therefore List01.Contains(i) breaks instantly.
instead of while loop here maybe it's better to use do .. while
int i;
do
{
i = Random.Range(1, 10);
} while (List01.Contains(i))
Answer by sacredgeometry · Dec 07, 2020 at 07:07 PM
This is very confused.
are you just trying to generate a list of random order ints between a range of values?
If so why not just create a list of integer values and then randomise the order of the list instead?
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class Test : MonoBehaviour
{
public static void Shuffle<T>(IList<T> ts)
{
var count = ts.Count;
var last = count - 1;
for (var i = 0; i < last; ++i)
{
var r = UnityEngine.Random.Range(i, count);
var tmp = ts[i];
ts[i] = ts[r];
ts[r] = tmp;
}
}
public Stack<int> NumberStack;
void Start()
{
var randomNumbers = Enumerable.Range(0, 10)
.ToList();
randomNumbers.Shuffle();
NumberStack = new Stack<int>(randomNumbers);
Debug.Log(NumberStack.Pop());
Debug.Log(NumberStack.Pop());
Debug.Log(NumberStack.Pop());
Debug.Log(NumberStack.Pop());
Debug.Log(NumberStack.Pop());
Debug.Log(NumberStack.Pop());
}
}
Answer by CmdrZin · Dec 07, 2020 at 09:06 PM
Try this.
public List<int> List01 = new List<int>();
// Update is called once per frame
void Start()
{
List01.Add(5);
getNewNumeber(5);
}
int getNewNumeber(int i)
{
int n = 0;
int result;
if (!List01.Contains(i)) // Check if there is an i already
{
result = i; // No. Ok to use.
List01.Add(i);
}
else
{
do
{
n = Random.Range(1, 10); // returns 1:9
} while (List01.Contains(n)); // keep trying to find a number not in the list.
List01.Add(n);
result = n; // New number
}
//make something with i like debugging
Debug.Log(n);
return result;
}
Why would you need to do it that way?
Surely you know all of the values. If you want them in a random order then just populate a collection with the values and randomise the order then iterate through them or pop them from a collection type that supports it Stack/ Queue.
The time complexity of this is horrendous.
I tend to agree. The whole approach doesn't make any sense. Seem the OP has 10 things to debug and he wants to randomly select them. His approach only works until the list fills up and will take longer each time to find the next number.
Your approach with a randomized list that is depopulated by one each use then fills up and randomizes again is much more efficient and useful.
Your answer
Follow this Question
Related Questions
What's the best way to find the smallest Vector3.Distance of an array of enemies? 2 Answers
Can I create a list with an int/float and a string? C# 2 Answers
How do I make a list of references to a variable? 2 Answers
C# List For Loop Only Returns Last Element 0 Answers
I can't get my list to loop and keep "spawning" prefabs 1 Answer