Jump to content

  • Log In with Google      Sign In   
  • Create Account

Awesome job so far everyone! Please give us your feedback on how our article efforts are going. We still need more finished articles for our May contest theme: Remake the Classics

Better random generation for spawning enemies in Flash game


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 SpaceJesus   Members   -  Reputation: 105

Like
0Likes
Like

Posted 29 June 2012 - 11:04 AM

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++;
}


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

Sponsor:

#2 mark ds   Members   -  Reputation: 241

Like
0Likes
Like

Posted 29 June 2012 - 01:51 PM

Search for "Mersenne Twister".

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

#3 DevLiquidKnight   Members   -  Reputation: 680

Like
0Likes
Like

Posted 29 June 2012 - 03:39 PM

You might try looking into, a linear feedback shift register.

Edited by DevLiquidKnight, 29 June 2012 - 03:45 PM.


#4 ApochPiQ   Moderators   -  Reputation: 7663

Like
2Likes
Like

Posted 29 June 2012 - 04:35 PM

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 :-)

#5 SpaceJesus   Members   -  Reputation: 105

Like
0Likes
Like

Posted 02 July 2012 - 03:52 PM

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

mathematical logic:


Posted Image




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS