percentage function - 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: percentage function (
/showthread.php?tid=553808)
percentage function -
pedrotvr - 31.12.2014
I wanted to make a function that when called her, she calculated a random number of 100%
Ai would fall 1%, 10%, 40%, and always falling larger numbers, I do not know what I did wrong, but always falls 12:10!
pawn Код:
public TestePCT(playerid) {
new Float: p = float(random(1000)/ 1000 * 100);
if(p <= 0.10)
{
SendClientMessage(playerid, -1, "0.10");
}
else if(p <= 1.90)
{
SendClientMessage(playerid, -1, "1.90");
}
else if(p <= 3.00)
{
SendClientMessage(playerid, -1, "3");
}
else if(p <= 5.00)
{
SendClientMessage(playerid, -1, "5");
}
else if(p <= 10.00)
{
SendClientMessage(playerid, -1, "10");
}
else if(p <= 15.00)
{
SendClientMessage(playerid, -1, "15");
}
else if(p <= 25.00)
{
SendClientMessage(playerid, -1, "25");
}
else if(p <= 40.00)
{
SendClientMessage(playerid, -1, "40");
}
return 1;
}
Re: percentage function -
rickisme - 31.12.2014
pawn Код:
new p = floatround(100 * random(100) / 100, floatround_round);
printf("percent : %d %", p);
or if you want float value
pawn Код:
new Float:p = 100 * random(100) / 100;
printf("percent : %0.2f %", p);
AW: percentage function -
Nero_3D - 31.12.2014
there is no reason to use floats here, leave the comma away and compare integer values which is faster
I use a random value of 10001 to get 0 - 10000 which represents the 100% with two fractional digits
pawn Код:
public TestePCT(playerid) {
new rand = random(10001); // 0 - 10000
if(rand <= 10)
return SendClientMessage(playerid, -1, "0.00 - 0.10");
if(rand <= 190)
return SendClientMessage(playerid, -1, "0.11 - 1.90");
if(rand <= 300)
return SendClientMessage(playerid, -1, "1.91 - 3.00");
if(rand <= 500)
return SendClientMessage(playerid, -1, "3.01 - 5.00");
if(rand <= 1000)
return SendClientMessage(playerid, -1, "5.01 - 10.00");
if(rand <= 1500)
return SendClientMessage(playerid, -1, "10.01 - 15.00");
if(rand <= 2500)
return SendClientMessage(playerid, -1, "15.01 - 25.00");
if(rand <= 4000)
return SendClientMessage(playerid, -1, "25.01 - 40.00");
return SendClientMessage(playerid, -1, "40.01 - 100.00");
}