Page 1 of 1

random number generator

PostPosted: Sun Sep 18, 2011 2:18 am
by Fojam
how would i generate a random number between 1 and 500?

correct this if its wrong but is it this?
Code: Select all
Math.floor(Math.random()*500);

Re: random number generator

PostPosted: Sun Sep 18, 2011 2:58 am
by jimmynewguy
Just use rand.
Code: Select all
int random = rand(499)+1;// gets a random number between 0 and maximum, that's why we have 499 and +1

Re: random number generator

PostPosted: Sun Sep 18, 2011 2:46 pm
by Fojam
thanks that works. so the one in parenthesis is the maximum and the + number is the minimum?

Re: random number generator

PostPosted: Sun Sep 18, 2011 2:53 pm
by savvy
you dont have to have a +, heres how it works
rand(500);
this would get a number ranging from 0 to 499 the +1 makes it range from 1 to 500.
you can use rand to get make any random thing, for example.. creating an actor in the view at a random point.

Code: Select all
x=view.x+rand(view.width);
y=view.y+rand(view.height);


:) savvy

Re: random number generator

PostPosted: Sun Sep 18, 2011 3:08 pm
by Fojam
and is there a way to save the variable of a random number so it will load it when the application starts?
if i saved the random number in a variable named random, could i save that somewhere so that it would load it at runtime and display it in a text actor?

Re: random number generator

PostPosted: Sun Sep 18, 2011 3:12 pm
by jimmynewguy
Fojam, Just use save/load vars

Re: random number generator

PostPosted: Sun Sep 18, 2011 6:17 pm
by Fojam
sorry been a while since ive used GE. i took a break to learn actionscript so yeah.

Re: random number generator

PostPosted: Sun Sep 18, 2011 7:58 pm
by savvy
yeah, make a variable called random..
random = rand(amount);
SaveVars("file","group");
(or whatever it is, i use my own save things)