SA-MP Forums Archive
random with precents? - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: random with precents? (/showthread.php?tid=598981)



random with precents? - Lirbo - 19.01.2016

For example if a player has in his file: Luck=10 then the random will be 10% of success.
how can i do this?


Re: random with precents? - M4D - 19.01.2016

PHP код:
new PlayerLuck[playerid];  // <------- Variable that player's luck stored in.
new rand random(100);
if(
<= rand && rand <= PlayerLuck[playerid]) return ... //> Success
else if(rand PlayerLuck[playerid]) return ... //> Fail 



Re: random with precents? - AmigaBlizzard - 19.01.2016

A bit shorter:
PHP код:
new PlayerLuck[playerid];  // <------- Variable that player's luck stored in.
if(random(100) < PlayerLuck[playerid])
   
//> Success (random gives a value from 0 to 9)
else
   
//> Fail (random gives a value from 10 to 99) 
"random" returns values from 0 to value-1 (checking if "random" was below 0 is useless), so with the code above it can return values from 0 to 99, and we check if the valid value is from 0 to 9 (this would be 10%).


Re: random with precents? - AbyssMorgan - 19.01.2016

PHP код:
stock bool:IsDropAllow(chance){
    if(
chance <= 0) return false;
    if(
chance >= 100) return true;
    if((
random(100)+1) <= chance) return true;
    return 
false;