Better random generation for spawning enemies in Flash game

Started by
3 comments, last by SpaceJesus 11 years, 9 months ago
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
Advertisement
Search for "Mersenne Twister".

I've used an implementation before and it's very good as a general randon number generator.
You might try looking into, a linear feedback shift register.
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 :-)

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

Thks. I still need add sounds and work with the game difficult/balance/progression, but made a new version:
http://mcn78.sites.u...m.br/st/st.html


I don´t know if I did the correct way to follow your

[background=rgb(250, 251, 252)]mathematical logic:[/background]



code.gif

This topic is closed to new replies.

Advertisement