/********************************************************************
 *
 * Implementation of the adaptive algorithm for determining
 * determining strings from substrings.
 * Prefix tree version.
 *
 * Auxiliary functions.
 *
 * (C) February - December 1994, Dimitris Margaritis, Steven Skiena
 *
* $Id: aux.c,v 1.3 1995/04/18 21:34:53 dmarg Exp dmarg $
 *
 ********************************************************************/

#include "includes.h"

/********************************************************************
 *
 * Return a random integer within the specified range i.e.
 * in [0, range).
 *
 ********************************************************************/

unsigned random_num(unsigned range)
{
static int seed = 0;

    if (seed == 0) {
        seed = 1;
        srandom((int) time(NULL));
    }

    return (unsigned) random() % range;
}

/********************************************************************
 *
 * Memory allocator.  Exits program if not enough memory exists.
 *
 ********************************************************************/

char *getmem(unsigned bytes)
{
char *ptr;
/*
static unsigned sum = 0;
*/

    if ((ptr = (char *) malloc(bytes)) == (char *) NULL) {
        perror("getmem()");
        fprintf(stderr, "Couldn't allocate %u byte(s).\n", bytes);
        exit(1);
    }
/*
    sum += bytes;
    printf("getmem(): bytes = %u, sum = %u\n", bytes, sum);
*/

    return ptr;
}

/********************************************************************/
