SA-MP Forums Archive
Chance code - 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: Chance code (/showthread.php?tid=349175)



Chance code - Da' J' - 08.06.2012

I couldn't find it anywhere, how could you script that something happens for lets say, with 50% possibility?

Like, with probability of 50% something happens, and with the rest probability it'll fail. How did you do it?


Re: Chance code - MP2 - 08.06.2012

Using random().

If you were to do 'random(2)' it will return either 0 or 1, 50% chance each, so you can do something if it's 0:

pawn Code:
if(random(2) == 0)
{
    // Do something
}
If you wanted a 25% chance you can swap out the 2 for a 4. If you want a 10 percent chance a 10 and so on.


Re: Chance code - Da' J' - 08.06.2012

Oh yeah, it was random()! Shit it was so easy, didn't remember. I just thought it was a huge mess of codes lmao. Thanks!