Maths help!

[SIZE=2]Hi all,
A question for all you math experts out there…
how do you make a function that is possibe according to a %
eg:for a stat system,there is a 60% chance to do -2 the enemy,
how do you hake him do that???
Thanks -a-lot!

It’s simple.

function f60(){
// 60 % chance of getting called
var nChance:Number = random(100)
f60 not called


f60 not called


thank you!

I’d consider revising it just to eliminate the depricated method call…

executeOnChance( 0.6, sayHello );

function sayHello():Void

// chance as a value from 0-1, ex: 50% = 0.5
function executeOnChance( chance:Number, method:Function ):Void
   var value:Number = Math.random();
   if (value < chance) method();

This makes the idea a little more reusable…


EDIT:\ Innapropriate use of the ternary operator