# Maths help!

**URL:** https://forum.kirupa.com/t/maths-help/186427
**Category:** flash
**Created:** [April 21, 2006, 4:44am UTC](https://forum.kirupa.com/t/maths-help/186427 "2006-04-21T04:44:01Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![lone\_wild\_wolf](https://avatars.discourse-cdn.com/v4/letter/l/3da27b/32.png) [@lone\_wild\_wolf](https://forum.kirupa.com/u/lone_wild_wolf)
#### Post date: [April 21, 2006, 4:44am UTC](https://forum.kirupa.com/t/maths-help/186427/1 "2006-04-21T04:44:01Z")

</div>

[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!  
[/SIZE]

---

<div class="post-metadata">

### Author: ![kookaburra](https://avatars.discourse-cdn.com/v4/letter/k/f475e1/32.png) [@kookaburra](https://forum.kirupa.com/u/kookaburra)
#### Post date: [April 21, 2006, 5:31am UTC](https://forum.kirupa.com/t/maths-help/186427/2 "2006-04-21T05:31:05Z")

</div>

It’s simple.

[AS]  
function f60(){  
// 60 % chance of getting called  
}  
var nChance:Number = random(100)  
if(nChance\<60){  
f60 not called  
f60();

}else{

```
f60 not called

```

}  
[/AS]

---

<div class="post-metadata">

### Author: ![lone\_wild\_wolf](https://avatars.discourse-cdn.com/v4/letter/l/3da27b/32.png) [@lone\_wild\_wolf](https://forum.kirupa.com/u/lone_wild_wolf)
#### Post date: [April 21, 2006, 10:00am UTC](https://forum.kirupa.com/t/maths-help/186427/3 "2006-04-21T10:00:18Z")

</div>

thank you!

---

<div class="post-metadata">

### Author: ![MichaelxxOA](https://avatars.discourse-cdn.com/v4/letter/m/9dc877/32.png) [@MichaelxxOA](https://forum.kirupa.com/u/MichaelxxOA)
#### Post date: [April 24, 2006, 4:07pm UTC](https://forum.kirupa.com/t/maths-help/186427/4 "2006-04-24T16:07:54Z")

</div>

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

```auto

executeOnChance( 0.6, sayHello );

function sayHello():Void
{
   trace("Hello");
}

// 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…  
TakeCare

\_Michael

EDIT:\ Innapropriate use of the ternary operator
