Random.Range but without some numbers

So I want Random.Range(1,5) Just for an example but without numbers that I’ll get later so basiclly if Random.Range picks number 3 for 3 times Random.Range cant pick that number again or sorta speaking something like that so anyway my question is: Is there a way to eleminate some parts of collection like R.R(1,5) \ (3) or something like that?

List numbersToChooseFrom = new List(new int{1,2,3,4,5});

...

void YourMethod()
{
    ...
    
    int randomNumber = numbersToChooseFrom[Random.Range(0, numbersToChooseFrom.Count)];

    ...
}

Whenever you want to remove one of the electible numbers, say you want to remove the number 3:

numbersToChooseFrom.Remove(3);

If you want to add another number, say number 7:

numbersToChooseFrom.Add(7);

I would populate a collection with the numbers you want and then use Random.Range for the index - this way you can also remove them (if they are in something like a list) if you do not want them to come up again.

List<int> rNumbers = new List<int> {0,3,5,6,7,8,9,10};
int rIndex = Random.Range(0, rNumbers.Count);
int rNumber = rNumbers[rIndex];

// If you don't want that number again
rNumbers.RemoveAt(rIndex);

Hope that helps =D

Hey!

I’ve created an asset designed exactly for that case (it’s paid).

Feel free to try it!