For the programmers...

February 25, 2017 12:24PM
It looks like Furey wrote a random-number generator for Merc based on an algorithm from Knuth. When Alander wrote ROM, he said he noticed streakiness in the Furey-written RNG and reverted to the POSIX system function random() while seeding it using srandom() with a seed of a bitwise XOR of the time_t long and the program's PID. The relevant bits are below:

/*
 * I've gotten too many bad reports on OS-supplied random number generators.
 * This is the Mitchell-Moore algorithm from Knuth Volume II.
 * Best to leave the constants alone unless you've read Knuth.
 * -- Furey
 */

/* I noticed streaking with this random number generator, so I switched
   back to the system srandom call.  If this doesn't work for you,
   define OLD_RAND to use the old system -- Alander */

#if defined (OLD_RAND)
static int rgiState[2 + 55];
#endif

void init_mm ()
{
#if defined (OLD_RAND)
    int *piState;
    int iState;

    piState = &rgiState[2];

    piState[-2] = 55 - 55;
    piState[-1] = 55 - 24;

    piState[0] = ((int) current_time) & ((1 << 30) - 1);
    piState[1] = 1;
    for (iState = 2; iState < 55; iState++)
    {
        piState[iState] = (piState[iState - 1] + piState[iState - 2])
            & ((1 << 30) - 1);
    }
#else
    srandom (time (NULL) ^ getpid ());
#endif
    return;
}



long number_mm (void)
{
#if defined (OLD_RAND)
    int *piState;
    int iState1;
    int iState2;
    int iRand;

    piState = &rgiState[2];
    iState1 = piState[-2];
    iState2 = piState[-1];
    iRand = (piState[iState1] + piState[iState2]) & ((1 << 30) - 1);
    piState[iState1] = iRand;
    if (++iState1 == 55)
        iState1 = 0;
    if (++iState2 == 55)
        iState2 = 0;
    piState[-2] = iState1;
    piState[-1] = iState2;
    return iRand >> 6;
#else
    return random () >> 6;
#endif
}

/*
 * Generate a random number.
 */
int number_range (int from, int to)
{
    int power;
    int number;

    if (from == 0 && to == 0)
        return 0;

    if ((to = to - from + 1) <= 1)
        return from;

    for (power = 2; power < to; power <<= 1);

    while ((number = number_mm () & (power - 1)) >= to);

    return from + number;
}
Subject Author Posted

A Quick Study of RNG for CF

Tolgrumm February 25, 2017 12:08PM

this thread makes me realize why i majored in finance and not computer science... nt

The Forsaken(VIP) February 27, 2017 11:07AM

How long did this run take? I ask because RNG being very fast matters for CF (n/t)

laearrist February 27, 2017 05:29AM

Verry little (details inside)...

Tolgrumm February 27, 2017 10:01AM

Re: A Quick Study of RNG for CF

Isildur(VIP) February 26, 2017 04:53AM

Re: A Quick Study of RNG for CF

Tolgrumm February 26, 2017 06:00AM

God you turn me on. (n/t)

Matrik February 26, 2017 07:32AM

Keep talking like that...

Tolgrumm February 26, 2017 07:47AM

I think you win the Matrik's Biggest ROFL From Dios award. (n/t)

Matrik February 26, 2017 08:13AM

Re: A Quick Study of RNG for CF

Kstatida February 26, 2017 01:49AM

Uh...

Tolgrumm February 26, 2017 02:51AM

lol :)

Kstatida February 26, 2017 03:16AM

Re: lol :)

Tolgrumm February 26, 2017 06:35AM

Random: It took a recent neutral char 13 tries to get protection evil from a potion. Those odds are 1 in 4096. (n/t)

saagkri February 25, 2017 05:06PM

Since I can't be bothered to do the maths

daurwyn(VIP) February 25, 2017 10:58PM

Huh - I don't need a calculator to know that you 100% lied about OOC shadow plane collusion.

Nurok February 27, 2017 06:36AM

There's nothing magical in getting 1 in 4000 chance case

Kstatida February 26, 2017 03:19AM

I once rolled doubles eight times in a row playing Monopoly (went to jail twice). Odds of that are 1 in 1,679,616. I thought it was pretty magical. (n/t)

saagkri February 26, 2017 06:28PM

Interestingly, if you remove the didgets 666 from 1,679,616, you get 1791, the year the U.S. established the National Bank as a...monopoly. Creepy. (n/t)

saagkri February 26, 2017 06:42PM

You just blew my mind man. NT

Sam February 27, 2017 01:38PM

Exactly...

Tolgrumm February 26, 2017 06:43AM

Dont forget that cf is not a single user

Quas February 25, 2017 01:53PM

Yes and no...

Tolgrumm February 25, 2017 03:56PM

For the programmers...

Tolgrumm February 25, 2017 12:24PM



Sorry, you do not have permission to post/reply in this forum.

Online Users

Guests: 100
Record Number of Users: 1 April 26, 2024
Record Number of Guests: 133 April 26, 2024