Skip to content

Instantly share code, notes, and snippets.

@Steellgold
Last active January 26, 2022 20:09
Show Gist options
  • Save Steellgold/ca717ffb1947e316862f000dcc24d9f9 to your computer and use it in GitHub Desktop.
Save Steellgold/ca717ffb1947e316862f000dcc24d9f9 to your computer and use it in GitHub Desktop.
Chance Function PHP
<?php
namespace App\Utils;
class ChancePercentage {
public function chancePercentage(array $array = [
"myFirstValue" => [
"chance" => 3,
"name" => "Money x40000",
"content" => "This is mythic rarity! if you have luck you win this!!"
],
"mySecondValue" => [
"chance" => 55,
"name" => "Bob Poop",
"content" => "This is just.. poop :/"
],
"myThreeValue" => [
"chance" => 80,
"name" => "Cool Item",
"content" => "This is a super item to win!"
],
]){
$random = mt_rand(1, 100);
$start = 0;
$before = 100;
foreach ($array as $data) {
$start += $data["chance"];
$after = $start - $random;
if (abs($after) < $before) {
$before = abs($after);
$dropData = $data;
}
}
echo "You win: " . $dropData["name"] . "\n" . $dropData['content'];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment