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

Random doesn't work

N

noé cambou

Guest
Hi,
When I use fonctions like random() or choose(), it's Always the same Numbers wich are given.

For exemple, with random(3), the program will give me 2,41 for sure the first time. Then 2,89 the second time. And 0,76 ; 1,94 etc...

So the random is not random ?! It's Strange…

In conclusion, my aleatory levels are always the same, games after games…

Can you help me ?
 
It's true. Random when it comes to computers is not really random, its usually called pseudo-random. (Fake Random).

When you call a random function, the computer is actually just performing a complicated mathematical function that generates a sequence of numbers that is the same every time.

However, there is a way to change the initial conditions of this mathematical sequence of numbers.

If you use the function called randomise() (only need to call it once) at the start of your game (Put it in the Creation Code of your first room in the game for example, or an initialisation object if you have one), the computer will give you a different sequence of pseudo-random numbers each time you run the game, giving the appearance of a fresh set of random numbers.
 

Mert

Member
I think the OP's talking about seeds random_set_seed

So, the random in Game Maker is not actually random. When you use random(x), it returns a number that's already generated in a sequence. This sequence is represented by a seed (Usually a long long number... / It actually generates a random number by the seed, but this is easier to explain). Game Maker does always give you the same seed value when you launch your game from IDE. This is intended for debugging purposes!

If you don't want this to happen, simply write the randomise in a Game Start Event. It simply generates a random seed for you.
Code:
randomise();
You can also provide a seed as well. With this way, you can generate your own RANDOM SEQUENCE.
Code:
random_set_seed(10);
You may ask why the seed then? Imagine, you have a multiplayer game like Minecraft and you must generate a block world. You'll have millions of blocks and each player has to get the same block positions. Instead of transferring block's position's data one by one, you may generate the whole world for everybody. Distribute to the seed number to every person, random_set_seed, and voila!
 

Mike

nobody important
GMC Elder
I'll just chime in a little here and say this is by design.

Imagine having an intermittent bug that happens when you get certain random numbers, but these can happen at any time - coz it's random.
By starting with the same random seed each time, means you bugs like these - which are a nightmare to track down, become a little easier to figure out. When things run exactly the same as the previous run, not just random bugs, but many other bugs are helped by a consistent random number generator running throughout the game.

And yes, to get rid of it, all you need to do is put "randomise();" at the start....
 
Top