• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

GameMaker Issue with irandom (Am I crazy?)

NeoShade

Member
Hoping somebody can help me out with something, because I think I'm going crazy over here.
I have the following code in the create event of an object:

Code:
randomize();

var lay_id = layer_get_id("Tiles");
var map_id = layer_tilemap_get_id(lay_id);
var max_height = tilemap_get_height(map_id);

tilemap_clear(map_id, 0);
var ymax = 0;

for (var ix = 0; ix < tilemap_get_width(map_id); ++ix) {
for (var iy = 0; iy < irandom(max_height); ++iy) {
    tilemap_set(map_id, 2, ix, iy);
    ymax = max(ymax, iy);
    }
    }

show_debug_message(ymax);
I also have the room restarting when I press the enter key. Other than that, the entire project is completely blank. The room is the default size for GMS2 (1024 * 768) and the tileset size is 16x16, so max_height == 48.

The problem that I'm having is that the irandom results are all in the lower half of the range (0-28ish).

I can't understand why this is happening, and I'm hoping somebody can shed some light on it for me.
 

NeoShade

Member
There is, that that souldn't be impacting here. I always make my for loops like this, as it's the way that the inbuilt GMS2 code snippet is written.

EDIT--- Ahhhh, hang on, I just figured it out. I've changed the code to this:

Code:
randomize();

var lay_id = layer_get_id("Tiles");
var map_id = layer_tilemap_get_id(lay_id);
var max_height = tilemap_get_height(map_id);

tilemap_clear(map_id, 0);
var ymax = 0;

for (var ix = 0; ix < tilemap_get_width(map_id); ++ix) {
    var r = irandom(max_height);
    for (var iy = 0; iy < r; ++iy) {
        tilemap_set(map_id, 2, ix, iy);
        ymax = max(ymax, iy);
        }
    }

show_debug_message(ymax);
Which has solved the problem. I guess that because I was pulling the random number every time the iy value was being checked in the loop, the values were being normalized somewhat. I'm not sure why that only gave me low range numbers rather than a middle range. Maybe somebody could explain that?
 

TheouAegis

Member
Because it goes up up up and as soon as the random reaches a lower value, it breaks. It's behaving exactly as you said.
 
Top