

rand()                   General Function                  rand()




Generate pseudo-random numbers

int rand()

rand generates  a set of  pseudo-random numbers.  It  returns in-
tegers in the range 0 to  32,767, and purportedly has a period of
2^32.  rand will always  return the same series of random numbers
unless you  first call the function srand  to change rand's seed,
or beginning-point.

***** Example *****

This example demonstrates  the functions rand and srand.  It uses
a threshold level  that is passed in argv[1] (default, MAXVAL/2),
the number  of trials passed  in argv[2] (default,  1,000), and a
seed passed in argv[3] (default, no seeding).


#define MAXVAL 32767    /* range of rand: [0,2^15-1] */



main(argc, argv)
int argc; char *argv[];
{
        register int i, hits, threshold, ntrials;



        hits = 0;
        threshold = (argc > 1) ? atoi(argv[1]) : MAXVAL/2;
        ntrials = (argc > 2) ? atoi(argv[2]) : 1000;
        if (argc > 3)
                srand(atoi(argv[3]));



        for (i = 1; i <= ntrials; i++)
                if (rand() > threshold)
                        ++hits;



        printf("%d values above %d in %d trials (%D%%).\n",
                hits, threshold, ntrials, (100L*hits)/ntrials);
}


***** See Also *****

general functions, srand()
_T_h_e _A_r_t _o_f _C_o_m_p_u_t_e_r _P_r_o_g_r_a_m_m_i_n_g, vol. 2




COHERENT Lexicon                                           Page 1


