random with precents?
#1

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

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 
Reply
#3

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%).
Reply
#4

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

Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)