Hey, I'm created StoneDrop plugin (dimonds, iron, coal etc. drops from stone). I have a question, how to set for example; chance for drop diamond from stone: 1:300 , iron ingot from stone 1:200 etc..? This is my code (all minerals have the same chnce [1:300]): PHP: if($e->getBlock()->getId() === Block::STONE) $e->setDrops([Item::get($ores[mt_rand(0, 300)])]); }}
PHP: mt_rand(1, $max) <= $perc returns true for $perc/$max chance. (Try putting $perc=0 and $perc=$max to prove this concept)
$perc is the numerator of chance. $max is the denominator of chance. Ignore what @CraftYourBukkit had said. Why did he even write 'case 5'? Whatever case you put there it has a 1/301 chance to run. For example, if you want to run the code for 7% chance, i.e. 7/100 chance: PHP: if(mt_rand(1, 100) <= 7) run_code(); If you want to run the code for 20/1000 chance: PHP: if(mt_rand(1, 1000) <= 20) run_code(); Or for 2% chance do_something() and 3% chance do_something_2() and another 5% chance do_something_3(): (but only one or none should happen) PHP: $rand = mt_rand(1, 100);if(($rand -= 2) <= 0){ do_something();}elseif(($rand -= 3) <= 0){ do_something_2();}elseif(($rand -= 5) <= 0){ do_something_3();} This avoids the need to recalculate the random number. If you want multiple to be able to happen, random with their L.C.M. and check with division and modulus arithmetic operators (/ and %) https://php.net/operators.arithmetic
Ok, so I don't want to run code with e. g. 7% chance, I want to set drop from stone;; 10% chance for drop iron ingot, 5% for drop diamond, 25% chance for drop coal [..] and so on.. Can you edit this; code for me?
Why case 5? Whatever case it is it still just has 1% 1/301 chance to run, so you're making it a bit misleading. In that case, it should be better to simply use if.
As I said, it just was an example. Of course you could use whatever case you want to, and I used case 5
Usually, in programming, if we want to use a random mnumber, we would just take 0 or 1 for integers, or special numbers like pi or golden ratio for floats. If you use a not-so-regular number, readers would tend to believe that it is there for some reason, and sometimes even suspect it to be you leaving some loopholes in the program for yourself.