Last active
January 26, 2022 20:09
-
-
Save Steellgold/ca717ffb1947e316862f000dcc24d9f9 to your computer and use it in GitHub Desktop.
Chance Function PHP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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