# Random Movement AND Random speed

**URL:** https://forum.kirupa.com/t/random-movement-and-random-speed/131452
**Category:** Uncategorized
**Created:** [December 15, 2004, 9:02pm UTC](https://forum.kirupa.com/t/random-movement-and-random-speed/131452 "2004-12-15T21:02:01Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![cerdo](https://avatars.discourse-cdn.com/v4/letter/c/7bcc69/32.png) [@cerdo](https://forum.kirupa.com/u/cerdo)
#### Post date: [December 15, 2004, 9:02pm UTC](https://forum.kirupa.com/t/random-movement-and-random-speed/131452/1 "2004-12-15T21:02:01Z")

</div>

So I got this from the tut on this site and it’s works great.

I can’t figure out though how to make the speed random as well as the movent.

I want all my little clips to move at random speeds in adition to direction.

Any clues?

## Thanks!

function getdistance(x, y, x1, y1) {  
var run, rise;  
run = x1-x;  
rise = y1-y;  
return (\_root.hyp(run, rise));  
}  
function hyp(a, b) {  
return (Math.sqrt(a_a+b_b));  
}  
MovieClip.prototype.reset = function() {  
//specify the width and height of the movie  
width = 600;  
height = 400;  
//-------------------  
var dist, norm;  
this.x = this.\_x;  
this.y = this.\_y;  
this.speed = Math.random()\*4+5;  
this.targx = Math.random()\*width;  
this.targy = Math.random()\*height;  
dist = \_root.getdistance(this.x, this.y, this.targx, this.targy);  
norm = this.speed/dist;  
this.diffx = (this.targx-this.x)\*norm;  
this.diffy = (this.targy-this.y)\*norm;  
};  
MovieClip.prototype.move = function() {  
if (\_root.getdistance(this.x, this.y, this.targx, this.targy)\>this.speed) {  
this.x += this.diffx;  
this.y += this.diffy;  
} else {  
this.x = this.targx;  
this.y = this.targy;  
if (!this.t) {  
this.t = getTimer();  
}  
if (getTimer()-this.t\>1000) {  
this.reset();  
this.t = 0;  
}  
}  
this.\_x = this.x;  
this.\_y = this.y;  
};

* * *

onClipEvent(enterFrame){  
move();  
}
