# \[help\] Random \_x position glitch

**URL:** <https://forum.kirupa.com/t/help-random--x-position-glitch/120102>\
**Category:** Uncategorized\
**Created:** [August 20, 2004, 5:07pm UTC](https://forum.kirupa.com/t/help-random--x-position-glitch/120102 "2004-08-20T17:07:05Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![St\_Nick](https://avatars.discourse-cdn.com/v4/letter/s/bb73d2/32.png) [@St\_Nick](https://forum.kirupa.com/u/St_Nick)\
**Post date:** [August 20, 2004, 5:07pm UTC](https://forum.kirupa.com/t/help-random--x-position-glitch/120102/1 "2004-08-20T17:07:05Z")

</div>

Hi all,

I’m currently trying to get a movieclip of mine to randomly pick a target x location, go to that location, and once it is there I need it to pick another location, rinse, repeat. My current code works for the first location (called targetx) but once it gets there it vibrates back and forth. I must be missing something…

```auto

onClipEvent(load){
var speed = 5;
this._y = 120;
this._x = 250;
var targetx = 250;
}
onClipEvent(enterFrame){ 
if (this._x > targetx){
this._x -= speed;
}else if (this._x < targetx){
this._x -= -speed;
}else if (this._x == targetx){
targetx = Math.random(1)*500;
}
}

```

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![St\_Nick](https://avatars.discourse-cdn.com/v4/letter/s/bb73d2/32.png) [@St\_Nick](https://forum.kirupa.com/u/St_Nick)\
**Post date:** [August 20, 2004, 8:41pm UTC](https://forum.kirupa.com/t/help-random--x-position-glitch/120102/2 "2004-08-20T20:41:52Z")

</div>

Nevermind, I fixed it. Here’s the updated code:

```auto

onClipEvent(enterFrame){
    if (this._x >= targetx + 5 ){
        this._x -= speed;
    }else if (this._x <= targetx - 5){
        this._x -= -speed;
    }else if ((Math.abs(this._x - targetx) <= 10)){
        targetx = Math.random(1)*500;
    }
}

```
