## [AS2] Probability / Chance

• 480 Views
• 3 Replies
Revelance
Revelance
• Member since: Sep. 11, 2010
• Offline.
Forum Stats
Member
Level 01
Artist
[AS2] Probability / Chance 2010-09-20 04:50:35

Hello there fellow Newgrounders, I have a question to ask about how to code Probability.

To be honest, I really have no idea how to do this, and have searched through the internet to find any useful information regarding this, but no luck.

How would you go about executing a function at lets say 23%?

Please point me in the right direction, and nothing too complicated is much appreciated.

_Revelance

ProfessorFlash
ProfessorFlash
• Member since: Oct. 6, 2007
• Offline.
Forum Stats
Member
Level 32
Programmer
Response to [AS2] Probability / Chance 2010-09-20 07:48:01

Pick a random number between 1 and 100. Check 'if' the picked number, X, is below your threshold. So if you want 23% chance, then run your function if X is 23 or below.

You can solve pretty much any problem you may have with AS3 by consulting the AS3 Language reference.

Coolio-Niato
Coolio-Niato
• Member since: Jun. 30, 2005
• Offline.
Forum Stats
Member
Level 28
Blank Slate
Response to [AS2] Probability / Chance 2010-09-20 14:42:20

``````if(Math.random() < .23){
function
}``````

Math.random() gives you a random value from 0 to 1. If that value is under .23, you are essentially dependent on the random value to determine the percentage of when the function should run.

Revelance
Revelance
• Member since: Sep. 11, 2010
• Offline.
Forum Stats
Member
Level 01
Artist
Response to [AS2] Probability / Chance 2010-09-20 21:08:39

Thank you very much guys, I appreciate it.