"True" Random Movement

Talk about making games.

"True" Random Movement

Postby plinydogg » Mon Dec 05, 2005 3:39 am

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
plinydogg
 
Posts: 104
Joined: Thu Aug 25, 2005 8:34 pm
Score: 0 Give a positive score

Postby twobob » Mon Dec 05, 2005 9:50 am

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;
}
twobob
 
Posts: 33
Joined: Sat Sep 10, 2005 10:29 am
Location: England
Score: 2 Give a positive score

Postby makslane » Mon Dec 05, 2005 12:21 pm

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)
makslane
Site Admin
 
Posts: 3947
Joined: Sat Apr 05, 2003 6:47 pm
Score: 182 Give a positive score

Postby plinydogg » Mon Dec 05, 2005 1:04 pm

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
plinydogg
 
Posts: 104
Joined: Thu Aug 25, 2005 8:34 pm
Score: 0 Give a positive score


Return to Game Development

Who is online

Users browsing this forum: No registered users and 1 guest

cron