Problem with Random Numbers

I want to make a PvZ like game and my problem is the spawning of enemies. I want it to be random but I want to make sure all lanes have a chance to spawn an enemy. I want to give all the lanes a number and when that number was chosen by the rng, on that lane will spawn an enemy. I want to make a rng that when it was already chosen, it was eliminated on the next roll of rng. This will ensure that all the numbers I put in the rng will be chosen. That will ensure that all lanes can spawn enemies. Hope someone will understand and help me with it.

You could get quite detailed and use weightings for each lane, but I think that’d be overkill.

A simpler solution would be to make an array of all the lane numbers, and then randomly rearrange the values. Then iterate through your array from first position to the end.

To give an even more random feel, have multiple entries for all the lanes, so you have a number of rounds before you reach the end of the list.

For example, say you have 10 lanes, then set the array size to 50, and have each lane number appear 5 times. Then run a randomiser over the array a few times to mix it all up.


1 Like

It seems like your idea was really great! But unfortunately I have limited knowledge about arrays and other stuffs. Can you show me an example of events when done in Gdevelop itself?

1 Like

Thanks! I will test it out. I will reply to this again if I have a problem.

An alternative method is just a loop where if the lane selected is full just keep adding 1 to the value until you find an empty lane (circling back to the beginning if you exceed the number of lanes)

1 Like

@MrMen and @krunkster what if I have 5 lanes and I have a timer that has a conditon after it reach 5, 10, 15, 20, 25 and when those time comes, It will spawn an enemy to a lane (which is random) but I want that all the lanes must be given an enemy when all this time and condition happens. I want it to be random but all the lanes must be given an enemy. It’s just like RandomInRange but every time that number was picked, it can’t be picked again in order to have a chance to a number to pick.

It’s hard to grok what you are describing exactly, but it sounds like you just need to keep track of which lanes you’ve already spawned in, and once you’ve spawned in all of them, set them all back to false and start picking randomly again.

Use the events I gave earlier, but with _loopCounter = 1 and _laneCounter = 5.

After you’ve used the 5 numbers, regenerate the next 5.