• 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.

Code to repeatedly subdivide a given range

L

ladlon

Guest
Okay, here's a fun one that I'm curious to hear opinions on...

I want to randomly choose a number (being used as the X position of an instance) between 0 and a set number (say 100).

I store that number into a variable (...in this case the first index of an array... Let's say XPos[0]....)

Now, I want to then pick a number between THAT number and 100, then store that in the second index of the array (Xpos[1]...)

Then, pick a number between that and 100, and store that in the third index....

...and so on. Probably 4 times or so.

The purpose of this odd procedure, is that I am subdividing a 'room' (horizontal platform) with randomly placed walls.

We start with the full 'platform/room'... and then divide it with a wall, so we now have two 'rooms'.

Then, I want the second wall to reside somewhere in the RIGHT room... and then divide the rightmost room from that 'split' with another wall.

Essentially, it's the same as me just creating 4 randomly (horizontally) placed walls along a platform... but the key difference, is that Each wall is to the right of the previous... so that the values in each index of the array are increasing, as we go up the index.

Now, I'm sure there's some way to take four random X values, and reassign them in ascending order (ex XPos[0] having the lowest value, and XPos[3] having the highest)... but I'm not sure how to do that.... and I'm wondering if the 'subdivide the rightmost room' method isn't better anyway.

One thing I would have to do, though, is only allow each random number choice to only use the first 3/4 of the 'room' to be split, otherwise there's a chance that it might choose a position far right in the room, not giving any tile room left to further subdivide.

I think I have the proper coding in my head... but I'm curious how experienced coders here would attack this concept.

Again, the main goal is to have 4(?) X positions stored in an array, with the values in ascending order (leftmost position to rightmost).


TLDR VERSION(!):

If I lost anyone on that, here's another explanation....
We want to randomly place (horizontal X position) four trees, with their X position stored in an array. The key is, to have the trees stored in the array so that the leftmost tree is the first index, the next tree (to the right of it) second, and so on, with the rightmost tree stored in the last index of the array.

Hope someone can help! I've very curious to hear how the pros would do this sort of thing.
 
L

ladlon

Guest
What I ended up doing, is simply divide the range (width of entire floor) by a set amount (ex. 4), then randomly placed each wall within each of those quadrants.

Besides being easy, it also assures that you don't end up with all the walls clumped together (...they each only occupy their own quadrant), yet because of the random X location within that, there's still plenty of variation in room size.

I'm still curious to hear how the previous challenge would be addressed, but now out of curiousity, and not need!
 
Top