ID:195033
 
// Title: Chance function.
// Credit to: Popisfizzy

/*
Just a few equivalent impelementations of a chance function.
This is similiar to the built-in prob function, but allows
for an explicit way of stating an x-in-y chance. For
example, to do a 1-in-1000 chance with prob(), you would
have to write prob(0.1), while with these you'd write chance(1, 1000). This is more useful when you have
probabilities not so easily represented.
*/


// Expanded proc version.
proc/chance(x, y)
x = round(x)
y = round(y)

if(x <= 0)
return 0

return (y - x + 1) <= rand(1, y)

// Compacted proc version.
proc/chance(x, y)
return (round(x) > 0) && ((round(y) - round(x) + 1) <= rand(1, round(y)))

// Preprocessor macro version.
#define chance(x, y) ((round(x) > 0) && ((round(y) - round(x) + 1) <= rand(1, round(y))))

// Text code:
mob/verb/Chance(x as num, y as num)
world << "[x]-in-[y] Chance: [chance(x, y)]"