All pastes #269250 Raw Edit

Stuff

public text v1 · immutable
·published 2006-12-06 12:33 UTC
rendered paste body
#include "stdlib.h"
#include "time.h"
#include "math.h"


/* ---------------------------------------- */
/* randint() - Give a pseudo-random number  */
/*                                          */
/* Args:                                    */
/* MIN = Minimum value of range from        */
/*        which to generate                 */
/* MAX = Max value of range from which to   */
/*         generate                         */
/* ---------------------------------------- */
int randint(int MIN, int MAX) {

                                double  value;
                                int             ival;
                                int             rnum;
                                double  r1, r2;
                                int             i;
                                static  int     RANDSTART=0;


        /* Seed the random number generator */
        if (! RANDSTART) {

                srand(time(NULL));
                RANDSTART = 1;
        } // end of if

        r1 = rand();
        r2 = rand();


        /* These 3 lines move the decimal and truncate, resulting
        in using the least-significant digits of the random
        number -- allows more variability */
        double rm;
        rm = RAND_MAX;
        value = 10000 * ((double) (r1*r2)) / (rm*rm);
        ival = (int) value;
        value = value - ival;


        /* This pushes the random number into the range we want */
        rnum = (int) (((MAX + 1) - MIN) * value + MIN);


        return rnum;

} /* end of randint() */