[C#] Random delete a number already generated

Good afternoon everybody :slight_smile: Before anything, i want to precise you that my English is very bad haha, i’m from Brasil, but i’m practicing, so please, be indulgent. So, I’m here because I have a problem, I’m making “a little video-game” to learn more about Unity, that game looks like a “quiz”. And in that game, the levels are chosen by random. The problem is, that I won’t that the same level comes more that one time. I want the level to be deleted when it was randomized. For example, if the level 2 is randomized, i want that the level 2 can not be randomize anymore. This is the extract of my script wich randomizes my levels aleatory:

SceneManager.LoadScene(“Level” + Random.Range(1, 21));

As you can see, i’m a beginner, but i didn’t found anything wich could help me in internet. Thanks you for helping

Sure, welcome to Unity.

Sometimes you just need to think out an idea… like maybe you fill a list from 0 - 20 (21 spots).
Now, from this list you pick a number (this is the scene you’ll load).
You then remove it from the list, so when you take a random number from the list, again, it won’t be there. :slight_smile:

1 Like

What methos5k suggested is a good approach.

Another approach is to create a list, randomize it using a shuffling algorithm, then iterate through it from the beginning.

The benefit this gives you is it allows you to “peek” at the next scene after the current scene. Kind of like in Tetris, you get a preview of what the next piece will be after the current piece finishes falling.

Hello ! Thanks you for your answers, i’ m att m’y work so i cant verify if m’y script us correct, but i dis that:

int[ ] mesNiveauxDispo = Enumerable.Range(1, 20).ToArray();


int monNiveau;
monNiveau = Random.Range(mesNiveauxDispo);
SceneManager.loadscene (monNiveau);

List foos = new List(mesNiveauxDispo);
foos.RemoveAt(index);
mesNiveauxDispo = foos.ToArray();

Do tou think that us correct ? And if its not could you show me what i have to change ? Thx u

You can order the level list by random and go through it in sequence

using System.Linq;
...
List<int> levels = Enumerable.Range(1, 20).OrderBy(i => Random.value).ToList()

Please, remember to use code tags when posting code on forum.