• Hello [name]! Thanks for joining the GMC. Before making any posts in the Tech Support forum, can we suggest you read the forum rules? These are simple guidelines that we ask you to follow so that you can get the best help possible for your issue.

Best way to pick random EVEN number within a specific range?

L

ladlon

Guest
Hello. I wanted to see what methods might be available for this...

I want to place object tiles at a random horizontal position on a ledge/floor in my platformer... but I would like their position to be only EVEN numbers, so that they are never adjacent to each other (...always at least one tile space between them).

I'm not too well-versed in all the GMS Pro commands, but also proper methods... but one way I figured would be with:

ItemX = TileSize * Floor(NumberOfTilesAcross / 2) * 2;

Where ItemX is the horizontal position of the instance, and NumberOfTilesAcross is the intended number of tiles across the playfield.


Is this an acceptable method, or is there some better way?
 
A

aGameDev

Guest
That seems mathematically sound.

Of course you will only have the objects on even tiles, and never on odd tiles.

If you wanted to have the possibility of objects on both even and odd tiles, but still make sure they are always separated by at least one tile/space, then just have a random chance of an object appearing on each tile (say 25% chance, depending on how many objects you want to appear), then check once for each tile along the ledge, going from left to right.

If an object is indicated on a tile, then you skip ahead 2 tiles before you check for the next object. That would make sure there's at least one free tile/space between any two objects.
 
L

ladlon

Guest
Ya, only even tiles would be fine (and intended).

Sure, I could always do a check before dropping an object onto the randomly chosen location (to make sure there's nothing directly beside it).... and that's how I would normally do it, actually... but, I wanted to see if there was a way to do it (mathematically) without the need of checks.

In this particular case, it would be to randomly cut holes/pits into the floor/ledge, but ensure they are only one tile wide (so they remain jumpable).

Okay, just checking to see if there wasn't some better way. Thanks!
 
H

Homunculus

Guest
Assuming you introduce the random element in there, it could work. It doesn’t prevent by itself getting the same random value twice though.

It may be an overkill if it’s a simple use case, but you could find a random position that’s guaranteed to avoid a collision by generating a ds_list if all possible tiles (for 10 tiles, numbers from 0 to 9). You randomly chose a value from the list and afterwards delete it along with the previous and next value. Repeat as much as needed.
 
L

ladlon

Guest
Yep, actually the way I deal with avoiding an endless loop (or at least a long delay) caused by the random location pick repeatedly choosing inappropriate locations is to have it move over to the right (or whatever) after a 'bad' choice, rather than pick a new, totally random location. That way, it will hit a good spot after one or more shift-overs.

In my stuff, everything on the playfield is random (platforms, item locations, etc). So, I have to really be smart about the build system.
 
Top