percentage function
#1

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;
}
Reply
#2

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

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");
}
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)