/*
 * Random numbers from 0 to system time ;^)
 *
 * You will need to change the includes for any other compiler than Maxon C
 */

#include <pragma/exec_lib.h>
#include <pragma/dos_lib.h>
#include <pragma/intuition_lib.h>
#include <stdlib.h>
#include <stdio.h>


struct Library *IntuitionBase = NULL;
LONG args[2] = {0, 0};
STRPTR tmplate = "HI=HIGH/K/N,LO=LOW/K/N\n";
char *verstring = "$VER: Randomizer 1.1 (16.07.97)";


/* If you use the Maxon C/C++ compiler, this program effectively does nothing,
   if you start it from workbench. */

void main(void)
{
    register LONG low = 0, high = 1, random, *num;
    struct RDArgs *rdargs;
    ULONG secs, mics;
    int rw = RETURN_FAIL;
    char numbuf[12];

    IntuitionBase = OpenLibrary("intuition.library", 37);
    if(IntuitionBase)
    {
        rw = RETURN_ERROR;

        rdargs = ReadArgs(tmplate, args, NULL);
        if(rdargs)
        {
            num = (LONG *) args[1];
            if(num)
            {
                low = *num;
            }

            num = (LONG *) args[0];
            if(num)
            {
                high = *num;
            }

            FreeArgs(rdargs);
        }

        CurrentTime(&secs, &mics);
        CloseLibrary(IntuitionBase);

        if(low >= high)
        {
            PrintFault(ERROR_BAD_NUMBER, "LOW >= HIGH !!! ");
            exit(rw);
        }

        // Create random-range
        high -= low;
        high++;

        // Create random number: range plus lower boundary
        secs += mics;
        secs %= high;
        random = secs + low;

        /* I am using my own sprintf() [utilizing RawDoFmt()], so your code
           will most likely be much bigger than mine. It is a safety kludge
           to distinguish a fake from the original. For your inconvenience,
           of course. You can find a smaller one in the autodoc text to
           exec.library/RawDoFmt(). */
        sprintf(numbuf, "%ld", random);

        if(SetVar("RANDOM", numbuf, -1, LV_VAR))
        {
            rw = RETURN_OK;
        }
    }

    exit(rw);
}
