Brained Washed
Brained Washed

Reputation: 703

Any idea for knowing if the certain percent of chance triggers

I have a game development project and I have specific character one of the skill of the character is that it has a 10% chance of doubling its attack.

Question: How can I trigger it?

Upvotes: 0

Views: 103

Answers (3)

ddinchev
ddinchev

Reputation: 34673

For example this code will double the value theoretically one in three times? It's hard for me to understand you.

$value = 200;
if (rand(1,3)===1) {
   $value*=2;
}

Or maybe this, for percents:

$value = 200;
$percent = 30;
$chance = rand(1, 100);
if ($chance <= $percent) {
    $value *= 2;
}

Upvotes: 0

PeeHaa
PeeHaa

Reputation: 72681

function doubleHit($percentChance = 30)
{
    if (mt_rand(1,100) <= $percentChance) {
        return true;
    }

    return false;
}

var_dump(doubleHit(35)); // will return either true / false

Note that this is only pseudorandom. Also note that this is faster / better than rand().

Upvotes: 1

Stefan Gi
Stefan Gi

Reputation: 450

I hope i understand ure question:

<?php
$random = rand(1,10);
if(($random == 1) || ($random == 2) || ($random == 3))
    $value += $value;
?>

Here you have a 30% chance to hit 1, 2 or 3.. and if its hit then your value gets doubled.

Ok now for your Update u just need a 10% chance? But okay:

<?php
$random = rand(1,10);
if($random == 1)
    $value += $value;
?>

Upvotes: 3

Related Questions