How does random() work? A question to think about
#1

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

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

^ WHAT THE FUCK IS THAT?



Gotta love the powers of scripting... so unbelievable
Reply
#4

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
Reply
#5

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.
Reply
#6

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

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
Reply
#8

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


Forum Jump:


Users browsing this thread: 1 Guest(s)