Jump to content
  • Advertisement
Sign in to follow this  
SpaceJesus

Better random generation for spawning enemies in Flash game

This topic is 2303 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am not a full programmer, but I doing a little retro game in Flash AS2.

I having a problem with the flash Random. It´s not random enough. I use random to generate the position X where the enemies spawn and the type of enemy (between 5 types).

Random in Flash generate clustered values. Sometimes a lot of one type of enemy spawn. There are tricks I can use to get a more varied random results?

Ex (not the entire code):





// random function between 5 types of enemies, with different frequence
this.onEnterFrame = function()
{
_root.pE = (Math.random()*1000);

if ( ((_root.pE >30) and (_root.pE <250)) or ((_root.pE >600) and (_root.pE <750)) or ((_root.pE >800) and (_root.pE <850)))
{
_root.qI="e01p";
}
if ( ((_root.pE >250) and (_root.pE <320)) or ((_root.pE >750) and (_root.pE <800)) )
{
_root.qI="e02p";
}
if ( ((_root.pE >320) and (_root.pE <400)) or ((_root.pE >850) and (_root.pE <920)) )
{
_root.qI="e03p";
}
if ( ((_root.pE >400) and (_root.pE <450)) or ((_root.pE >950) and (_root.pE <1000)) )
{
_root.qI="e04p";
}
if ( ((_root.pE >0) and (_root.pE <30)) or ((_root.pE >920) and (_root.pE <950)) )
{
_root.qI="e05p";
}


//control how much ships are generate each time
if(_root.iTotalShips <10)
{
_root.iEnemies++;
_root.attachMovie(_root.qI, "Enemy" + _root.iEnemies, _root.getNextHighestDepth());
_root["Enemy" + _root.iEnemies]._x = 20+random((_root.horizontalspace)-60);
_root["Enemy" + _root.iEnemies]._y = -30 - (random(400));
_root.iTotalShips++;
}

[/quote]

Link for the game (not finished):
http://mcn78.sites.uol.com.br/Game1m.html

Share this post


Link to post
Share on other sites
Advertisement
Search for "Mersenne Twister".

I've used an implementation before and it's very good as a general randon number generator.

Share this post


Link to post
Share on other sites
The problem is likely not the random number generator itself, but the way you use the random values from it. Note how you have several intervals that will cause enemy type 1 to spawn: (30, 250) and (600, 750) and (800, 850). It looks to me like you're trying to implement a weighted random (so certain types are more common than others) but this isn't the best way to do that.

A more mathematically sound result can be obtained by doing the following:

- Assign a percentage of "likeliness" to each enemy category
- Make sure all the percentages add up to 100%
- Generate a random number from 0 to 100
- If the number is less than the least likely enemy type's percentage, use that type
- Subtract the least likely enemy type's percentage from the number
- If the number is less than the next enemy type's percentage, use that type
- Repeat until you have chosen an enemy

You can also eliminate the subtraction steps by testing for [0, 10) and [10, 20) and so on instead of always looking for 0-n where n is a probability number.


That should eliminate a lot of your clumping issues :-)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!