# Math.random.whats wrong with my code?!

**URL:** <https://forum.kirupa.com/t/math-random-whats-wrong-with-my-code/44411>\
**Category:** Uncategorized\
**Created:** [March 3, 2004, 4:38pm UTC](https://forum.kirupa.com/t/math-random-whats-wrong-with-my-code/44411 "2004-03-03T16:38:57Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![joecastro](https://avatars.discourse-cdn.com/v4/letter/j/df788c/32.png) [@joecastro](https://forum.kirupa.com/u/joecastro)\
**Post date:** [March 3, 2004, 4:38pm UTC](https://forum.kirupa.com/t/math-random-whats-wrong-with-my-code/44411/1 "2004-03-03T16:38:57Z")

</div>

Why is it doing this??

What I want is for my mc to slide to a new random x position when it is less than 5 pixels away from its target, it keeps doing this. the problem is for some bloody &_£$(()_\* reason my random numbers only keep going up!! why is this?? i want to produce random numbers between 1 and 770 - this is my code…

Please somebody help me please…

onClipEvent (enterFrame) {  
\_root.xdif = \_root.targetx-\_x;  
if (\_root.xdif\<5) {  
\_root.targetx = Math.floor(Math.random()\*(770))+1;  
}  
if (\_root.xdif\<5) {  
this.\_x = null;  
} else {  
this.\_x += (\_root.xdif/50);  
}  
}
