# Loop trouble

**URL:** https://forum.kirupa.com/t/loop-trouble/331259
**Category:** flash
**Created:** [June 23, 2013, 4:45pm UTC](https://forum.kirupa.com/t/loop-trouble/331259 "2013-06-23T16:45:10Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![Gedicus](https://avatars.discourse-cdn.com/v4/letter/g/46a35a/32.png) [@Gedicus](https://forum.kirupa.com/u/Gedicus)
#### Post date: [June 23, 2013, 4:45pm UTC](https://forum.kirupa.com/t/loop-trouble/331259/1 "2013-06-23T16:45:10Z")

</div>

Hey guys, I am having a bit of trouble creating enemies at random points on my map, I want them to be contained within a certain y-axis range, I have made this code and it randomly generates then overwrites endless instances of the enemy sprite within the range. HELP!

//Loop:

public function spawnbarb(event:Event):void  
{  
var rand:int = 350;

```
        for(var count:int = 0; count &lt;= 2; count++)
        {
            
            if(rand != barb.y)
            {
                barb.x = 400;
            rand = Math.floor(Math.random() * 200 + 225); //Learn how to generate a new random number each iteration of the loop.
            barb.y = rand;
            stage.addChild(barb);
            barb.name = "barb" + count;
            trace("barb.name = "+ barb.name);trace("rand = " + rand); trace ("barb.y = " + barb.y);
            }
            else if(rand == barb.y)
            {
                rand = rand = Math.floor(Math.random() * 295 + 255);
                trace("rand in while = " + rand)
            }
            
        }
```
