Обучение/Помощь новичкам | Рандом с вероятностью
Тема закрыта by
[Андрей]
Причина: Неактуально.
Причина: Неактуально.
Всем привет, такой вопрос...
Есть 4 значения:
----
Как сделать чтобы они выпадали рандомно но с заданным шансом, test1 выпадал с шансом 20%, test2 с 30%, test3 с 45% и test4 с 60%.
Желательно примером помочь :)
Есть 4 значения:
$this->getLevel()->setBlock($this,(test1), true);
$this->getLevel()->setBlock($this,(test2), true);
$this->getLevel()->setBlock($this,(test3), true);
$this->getLevel()->setBlock($this,(test4), true);
$this->getLevel()->setBlock($this,(test2), true);
$this->getLevel()->setBlock($this,(test3), true);
$this->getLevel()->setBlock($this,(test4), true);
----
Как сделать чтобы они выпадали рандомно но с заданным шансом, test1 выпадал с шансом 20%, test2 с 30%, test3 с 45% и test4 с 60%.
Желательно примером помочь :)
<?php
function probability($massif, $percent)
{
if (!is_array($massif) || count($massif) < 1) return false;
$sum = 0; $result = null;
do{
foreach ($massif as $i => $data) {
$sum += $percent[$i];
if (rand(0, $sum) < $percent[$i]) {
$result = $data;
}
}
} while (is_null($result));
return $result;
}
?>
function probability($massif, $percent)
{
if (!is_array($massif) || count($massif) < 1) return false;
$sum = 0; $result = null;
do{
foreach ($massif as $i => $data) {
$sum += $percent[$i];
if (rand(0, $sum) < $percent[$i]) {
$result = $data;
}
}
} while (is_null($result));
return $result;
}
?>
DELETED
5 октября 2017, в 13:28
Delete
$rand = rand(1, 155);
if ($rand >= 1 && $rand <=20) {
1
} elseif ($rand >= 21 && $rand <=50) {
2
} elseif ($rand >= 51 && $rand <=95) {
3
} elseif ($rand >= 96 && $rand <=155) {
4
}
.
________
посл. ред. 05.10.2017 в 14:02; всего 1 раз(а); by _[LMNTRIX]_
________
посл. ред. 05.10.2017 в 14:02; всего 1 раз(а); by _[LMNTRIX]_
sindronik , пост 3
function asd($percent)
{
$a = 100 - $percent;
$b = mt_rand(1,100);
if($b >= $a) { $c = 1; } else { $c = 0; }
return $c;
}
if(asd('20%')) { echo 'win'; } else { echo 'lose'; } // test1
if(asd('30%')) { echo 'win'; } else { echo 'lose'; } // test2
if(asd('45%')) { echo 'win'; } else { echo 'lose'; } // test3
if(asd('60%')) { echo 'win'; } else { echo 'lose'; } // test4
{
$a = 100 - $percent;
$b = mt_rand(1,100);
if($b >= $a) { $c = 1; } else { $c = 0; }
return $c;
}
if(asd('20%')) { echo 'win'; } else { echo 'lose'; } // test1
if(asd('30%')) { echo 'win'; } else { echo 'lose'; } // test2
if(asd('45%')) { echo 'win'; } else { echo 'lose'; } // test3
if(asd('60%')) { echo 'win'; } else { echo 'lose'; } // test4