SA-MP Forums Archive
How does random() work? A question to think about - 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: How does random() work? A question to think about (/showthread.php?tid=309869)



How does random() work? A question to think about - Jochemd - 09.01.2012

Hello,

I was wondering a bit how the server actually picks a number when using random. I can't even imagine how it works? How is it made?

Jochem


Re: How does random() work? A question to think about - Babul - 09.01.2012

i dont remember which method is used by samp, but have a look here:
http://en.wikipedia.org/wiki/Pseudo-...mber_generator


Re: How does random() work? A question to think about - vincee - 09.01.2012

^ WHAT THE FUCK IS THAT?



Gotta love the powers of scripting... so unbelievable


Re: How does random() work? A question to think about - Wesley221 - 09.01.2012

The rabbit in your computer points at a number, and that will be the random number which will come out of the random() function. Most logical reason found for this


Re: How does random() work? A question to think about - RyDeR` - 09.01.2012

This is how random in PAWN works:
pawn Код:
#define INITIAL_SEED  0xcaa938dbL
static unsigned long IL_StandardRandom_seed = INITIAL_SEED; /* always use a non-zero seed */
#define IL_RMULT 1103515245L

static cell AMX_NATIVE_CALL core_random(AMX *amx,const 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 && !defined __ICC430__
        if (IL_StandardRandom_seed == INITIAL_SEED)
            IL_StandardRandom_seed=(unsigned long)time(NULL);
    #endif

    (void)amx;

    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;
}
Neat code if you ask me.


Re: How does random() work? A question to think about - Jochemd - 09.01.2012

Neat code or not, this random stuff is beyond me.


Re: How does random() work? A question to think about - Jochemd - 09.01.2012

Quote:
Originally Posted by ******
Посмотреть сообщение
That's interesting! I didn't realise "random(0)" generated a full 31-bit random number instead of one constrained to a given range.
Apparently, regarding the code.

Btw ******, are you a bot? =D


Re: How does random() work? A question to think about - Jochemd - 09.01.2012

Quote:
Originally Posted by ******
Посмотреть сообщение
No, why would you think that?
Because your location is 629 =D