|
|
coherent
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
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.