I want to make players get 20% chance of getting diamonds and 80% chance of getting dirt. but i tried PHP: rand(1,100);if($rand =< 20){20%}elseif($rand > 20){80%} but they said this one is bad
This has a 1% (yeah, no "pseudo random") chance of returning any number between 1 and 100. PHP: $rand = rand(1, 100); So this will have a 20% chance of returning true. PHP: if($rand <= 20){ return true;}return false;
What you can do is recalculate a random number every time. That makes it a little more random. PHP: if(rand(1, 100) <= 20){}elseif(rand(1, 100) <= 80){}
PHP: function chance(int $chance, callable $high, callable $low){ $rand = rand(1, 100); return $rand > $chance ? $low() : $high();}chance(20, function(){ echo "give the players diamonds";}, function(){ echo "give em dirt";});
That would be 33.3% chance. 20 and 80 percent would be rand(1, 5) so each number has a 20% chance of being called (20% = rand(1, 5) === 1, 80% = rand(1, 5) !== 1).