AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   %, chance, random (https://forums.alliedmods.net/showthread.php?t=188457)

Heyka 06-26-2012 15:14

%, chance, random
 
Hello guys!
I'm wondering how to calculate ACCURATE chance to do something. At the moment I use that script but it's lame and doesn't calculate right chance.
Code:

       
cvar_slash_chance = register_cvar("bs_slash_chance", "33")

static random_num0
random_num0 = random_num(0, 100)
if(random_num0 <= get_pcvar_num(cvar_slash_chance))

Of course it's just cut from whole code.

Can anyone manage how to be more accurate with setting %chance?

Exolent[jNr] 06-26-2012 15:18

Re: %, chance, random
 
That is how to do it. If you want an alternative, have a look at the random functions from xs.inc.

Code:

xs_seed(seed)
xs_irand()
Float:xs_frand()
xs_irand_range(pmin, pmax)


Heyka 06-26-2012 15:31

Re: %, chance, random
 
Sorry, but I'm new in Pawn and I don't know really what structure should I build. Would you help me?

ConnorMcLeod 06-26-2012 15:44

Re: %, chance, random
 
so xs_irand_range gives better results than random_num i presume ?

Heyka 06-26-2012 15:48

Re: %, chance, random
 
I bet. But can you give me example of usage?

Exolent[jNr] 06-26-2012 16:25

Re: %, chance, random
 
Quote:

Originally Posted by ConnorMcLeod (Post 1736717)
so xs_irand_range gives better results than random_num i presume ?

It is the same, I presume:

Code:

static cell AMX_NATIVE_CALL core_random(AMX *amx,cell *params)
{
    unsigned long lo, hi, ll, lh, hh, hl;
    unsigned long result;

    /* one-time initialization (or, mostly one-time) */
    #if !defined SN_TARGET_PS2 && !defined _WIN32_WCE
        if (IL_StandardRandom_seed == INITIAL_SEED)
            IL_StandardRandom_seed=(unsigned long)time(NULL);
    #endif

    lo = IL_StandardRandom_seed & 0xffff;
    hi = IL_StandardRandom_seed >> 16;
    IL_StandardRandom_seed = IL_StandardRandom_seed * IL_RMULT + 12345;
    ll = lo * (IL_RMULT  & 0xffff);
    lh = lo * (IL_RMULT >> 16    );
    hl = hi * (IL_RMULT  & 0xffff);
    hh = hi * (IL_RMULT >> 16    );
    result = ((ll + 12345) >> 16) + lh + hl + (hh << 16);
    result &= ~LONG_MIN;        /* remove sign bit */
    if (params[1]!=0)
        result %= params[1];
    return (cell)result;
}

The difference is that you can provide a seed.

ConnorMcLeod 06-26-2012 17:03

Re: %, chance, random
 
I though seed was hardcoded seeing xs code.

Exolent[jNr] 06-26-2012 17:16

Re: %, chance, random
 
Quote:

Originally Posted by ConnorMcLeod (Post 1736775)
I though seed was hardcoded seeing xs code.

It is if you don't change it with xs_seed().

Heyka 06-26-2012 17:30

Re: %, chance, random
 
I feel like
http://www.gagful.com/uploads/2012_4..._doing_gag.jpg
when reading it. Can anyone put new function and cvar, please?

fysiks 06-26-2012 18:54

Re: %, chance, random
 
Quote:

Originally Posted by Heyka (Post 1736791)
I feel like when reading it. Can anyone put new function and cvar, please?

Your original code will work just fine. However, if you want the code to be [more] correct, you need to change the 100 to 99.


All times are GMT -4. The time now is 06:15.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.