|
|
1.1 ! root 1: ! 2: ! 3: rand() General Function rand() ! 4: ! 5: ! 6: ! 7: ! 8: Generate pseudo-random numbers ! 9: ! 10: int rand() ! 11: ! 12: rand generates a set of pseudo-random numbers. It returns in- ! 13: tegers in the range 0 to 32,767, and purportedly has a period of ! 14: 2^32. rand will always return the same series of random numbers ! 15: unless you first call the function srand to change rand's seed, ! 16: or beginning-point. ! 17: ! 18: ***** Example ***** ! 19: ! 20: This example demonstrates the functions rand and srand. It uses ! 21: a threshold level that is passed in argv[1] (default, MAXVAL/2), ! 22: the number of trials passed in argv[2] (default, 1,000), and a ! 23: seed passed in argv[3] (default, no seeding). ! 24: ! 25: ! 26: #define MAXVAL 32767 /* range of rand: [0,2^15-1] */ ! 27: ! 28: ! 29: ! 30: main(argc, argv) ! 31: int argc; char *argv[]; ! 32: { ! 33: register int i, hits, threshold, ntrials; ! 34: ! 35: ! 36: ! 37: hits = 0; ! 38: threshold = (argc > 1) ? atoi(argv[1]) : MAXVAL/2; ! 39: ntrials = (argc > 2) ? atoi(argv[2]) : 1000; ! 40: if (argc > 3) ! 41: srand(atoi(argv[3])); ! 42: ! 43: ! 44: ! 45: for (i = 1; i <= ntrials; i++) ! 46: if (rand() > threshold) ! 47: ++hits; ! 48: ! 49: ! 50: ! 51: printf("%d values above %d in %d trials (%D%%).\n", ! 52: hits, threshold, ntrials, (100L*hits)/ntrials); ! 53: } ! 54: ! 55: ! 56: ***** See Also ***** ! 57: ! 58: general functions, srand() ! 59: _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 ! 60: ! 61: ! 62: ! 63: ! 64: COHERENT Lexicon Page 1 ! 65: ! 66:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.