Page 1 of 1

"True" Random Movement

PostPosted: Mon Dec 05, 2005 3:39 am
by plinydogg
Hello all,

I just purchased Game Editor a few days ago, so this is my first post!

My question is this: I have an "enemy" that I need to move randomly. While I think I understand the general syntax required, the enemy does the same thing (i.e., goes the same direction) every time.

I have pasted the code I used below. Basically, what I did was create random variables that are ultimately assigned to xvelocity and yvelocity. Then, I created a second set of random variables. Depending on whether the value returned is 1 or 0, xvelocity and yvelocity will be either positive or negative. Here is the code I used:

int xvel = rand(3);
int yvel = rand(3);
int xposorneg = rand(1);
int yposorneg = rand(1);

if(xposorneg==0)
{
xvelocity = 0-xvel;
}
else
{
xvelocity = xvel;
}

if(yposorneg==0)
{
yvelocity = 0-yvel;
}
else
{
yvelocity = yvel;
}


Every time I run it, the enemy moves to the same place (the upper left-hand corner of the screen). In other words, the movement is not random. I read somewhere in the documentation that "random" is really "pseudo random." Is that what's going on here? Is there no way to get "true" random movement?

I appreciate the help in advance, and I am very excited about working with this wonderful tool!!

Thank you,

Plinydogg

PostPosted: Mon Dec 05, 2005 9:50 am
by twobob
Try the following script:-

int xvel = round(rand(3));
int yvel = round(rand(3));
int xposorneg = round(rand(1));
int yposorneg = round(rand(1));
if(xposorneg==0)
{
xvelocity = 0-xvel;
}
else
{
xvelocity = xvel;
}

if(yposorneg==0)
{
yvelocity = 0-yvel;
}
else
{
yvelocity = yvel;
}

PostPosted: Mon Dec 05, 2005 12:21 pm
by makslane
The rand(1) function will return a random number between 0.0 and 1.0, not only the integers 0 and 1.

So, use the round function to get integer results.
(Using int will truncate the results, so you will have a little chance to get the upper limit)

PostPosted: Mon Dec 05, 2005 1:04 pm
by plinydogg
Perfect! That did the trick! Thanks a lot to both of you! I'm sure I will be asking for help again in a little while =)

Plinydogg