Home > OS >  How do I exclude certain numbers from Random.Range?
How do I exclude certain numbers from Random.Range?

Time:12-09

I'd like to randomly create items in a location that doesn't overlap.

How do I exclude values whenever I use Random.Range in Unity?

CodePudding user response:

Each location has an array or list of created items.

Use random numbers to "shuffle" the items so they appear in random locations.

They will never be duplicates or overlap because you set the starting lists. They will be random because you shuffle the "known" list randomly.

Hope that makes sense.

CodePudding user response:

You can do this several ways, how I usually approach this is to save the values of the generated random. Check in your loop if the next random is equal to the saved random, return then loop again.

  • Related