# Weird math.random() problem

**URL:** <https://forum.kirupa.com/t/weird-math-random-problem/260321>\
**Category:** flash\
**Created:** [May 14, 2008, 1:18pm UTC](https://forum.kirupa.com/t/weird-math-random-problem/260321 "2008-05-14T13:18:16Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![hapimp](https://avatars.discourse-cdn.com/v4/letter/h/b5a626/32.png) [@hapimp](https://forum.kirupa.com/u/hapimp)\
**Post date:** [May 14, 2008, 1:18pm UTC](https://forum.kirupa.com/t/weird-math-random-problem/260321/1 "2008-05-14T13:18:16Z")

</div>

Hi all.

I’m working on a little project in which some movieclips are to move random between certain X and Y values. Basically Ive just tried to make [this](http://www.tutorio.com/tutorial/flash-basic-random-motion) into AS3 code.

This is what it looks like:

```auto
this.addEventListener(Event.ENTER_FRAME, enterFrameHandler);

var acceleration:Number = 10;
var ranx:Number = (Math.round(Math.random()*358))+59;
var rany:Number = (Math.round(Math.random()*72))+64;
    
function newpos():void
{
    trace("newpos");
    ranx = (Math.round(Math.random()*358))+59;
    rany = (Math.round(Math.random()*72))+64;
}

function enterFrameHandler(event:Event)
{
    this.x += ((ranx-this.x)/acceleration);
    this.y += ((rany-this.y)/acceleration);
    if (Math.round(this.x) == ranx || Math.round(this.y) == rany) 
    {
        newpos();
    }        
}

```

My problem, then, is this: I need to change the acceleration var to get the movieclip to move slower. However, when i set it to 12 og higher the movieclip will only generate 3 or 4 new coordinates, and then stop completely.

Does anyone know why this is?

Ive attached my .fla so you all can see for yourselves. The code is in the first frame _the movieclips_ timeline.

I hope you can all help as im pretty new to actionscripting.

Thanks,  
Esben

---

<div class="post-metadata">

**Author:** ![Felixz](https://avatars.discourse-cdn.com/v4/letter/f/ecb155/32.png) [@Felixz](https://forum.kirupa.com/u/Felixz)\
**Post date:** [May 14, 2008, 2:59pm UTC](https://forum.kirupa.com/t/weird-math-random-problem/260321/2 "2008-05-14T14:59:37Z")

</div>

try just use some tween engine and randomize target coords

---

<div class="post-metadata">

**Author:** ![hapimp](https://avatars.discourse-cdn.com/v4/letter/h/b5a626/32.png) [@hapimp](https://forum.kirupa.com/u/hapimp)\
**Post date:** [May 14, 2008, 4:03pm UTC](https://forum.kirupa.com/t/weird-math-random-problem/260321/3 "2008-05-14T16:03:26Z")

</div>

Thanks for your reply Felixz, but i am not sure about what you mean by ‘tween engine’, could you provide a link to an example of one such? Remember that i have almost no experience with AS, so i mostly cut n paste stuff i find on the Internet.

Also, i kind of like the code ive used, does anyone know why i am not able to change the acceleration var above 11 without the whole thing going wrong?

Esben

---

<div class="post-metadata">

**Author:** ![hapimp](https://avatars.discourse-cdn.com/v4/letter/h/b5a626/32.png) [@hapimp](https://forum.kirupa.com/u/hapimp)\
**Post date:** [May 14, 2008, 4:56pm UTC](https://forum.kirupa.com/t/weird-math-random-problem/260321/4 "2008-05-14T16:56:15Z")

</div>

Problem solved courtesy of mr. Bombdogs at [actionscript.org](http://actionscript.org):

> **[ActionScript.org Forums - Powered by vBulletin](http://www.actionscript.org/forums/index.php)**
>
> ActionScript.org Forums

---

<div class="post-metadata">

**Author:** ![Felixz](https://avatars.discourse-cdn.com/v4/letter/f/ecb155/32.png) [@Felixz](https://forum.kirupa.com/u/Felixz)\
**Post date:** [May 14, 2008, 7:44pm UTC](https://forum.kirupa.com/t/weird-math-random-problem/260321/5 "2008-05-14T19:44:17Z")

</div>

u can use Tween class which is built in.  
Or TweenLite, Tweener, Fuse etc.  
U can search for them or u can go directly [here](http://tweenlite.com)
