Login | Register

Random number

July 25, 2006, 15:00 by Calle
[loading]
-->
Random numbers and probability is simple to deal with in Game Maker. Combining the function random() with a simple if statement and correct condition it will be no problem. Let me show you how you might do actions on random basis:

color[0] = c_black;
color[1] = c_gray;
color[2] = c_blue;
color[3] = c_green;
color[4] = c_yellow;
color[5] = c_maroon;
draw_text_color(x,y,string,color[floor(random(6))], color[floor(
random(6))], color[floor(random(6))], color[floor(random(6))],alpha)


And this will create a string with random colour. You might also do a lottery the same way. Say there is 10 possibilities, and you could initialize the amount of the prize into a variable and then give the player money through the code cash += prize[floor(random(11))] or something like that.

Probability is just as simple. Say for an example that you want two colours only and you want black to be chosen three times out of four, then you could initialize the array like this:

color[0] = c_black;
color[1] = c_black;
color[2] = c_black;
color[3] = c_gray;


But you could also do it like this:
nr = floor(random(5));
if (nr>3) {
colour = c_black;
}


…and these are the basics.

http://gmtutorials.com/files/randomex.gm6

Comments

Loading comments... [loading]
.
Users logged in:

game maker articles, game maker examples, game maker tutorials, gmtutorials, game maker questions and answers, game maker crash course, how to create games