C语言基础教程课件(英文)ch06要点.ppt
文本预览下载声明
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * A First Book of ANSI C, Fourth Edition * Standard Library Functions The standard library consists of 15 header files Before using these functions, you must know The name of each available function The arguments required by each function The data type of the result (if any) returned by each function A description of what each function does How to include the library containing the desired function #include header-file-name A First Book of ANSI C, Fourth Edition * Mathematical Library Functions A First Book of ANSI C, Fourth Edition * Mathematical Library Functions (continued) A First Book of ANSI C, Fourth Edition * The rand() and srand() Functions Random numbers are a series of numbers whose order cannot be predicted Pseudorandom numbers are not really random, but are sufficiently random for the task at hand All C compilers provide two functions for creating random numbers: rand() and srand(), defined in the stdlib.h header file rand() produces random numbers in the range 0 rand() RAND_MAX srand() provides a starting “seed” value for rand() A First Book of ANSI C, Fourth Edition * The rand() and srand() Functions (continued) A First Book of ANSI C, Fourth Edition * Scaling The method for adjusting the random numbers produced by a random-number generator to reside within a specified range is called scaling To scale a random number as an integer value between 1 and N: 1 + (int)rand() % N To produce a random integer between the numbers a and b: a + (int)(rand() % (b - a + 1)) A First Book of ANSI C, Fourth Edition * Coin Toss Simulation A First Book of ANSI C, Fourth Edition * Coin Toss Simulation (continued) A First Book of ANSI C, Fourth Edition * Coin Toss Simulation (continued) A First Book of ANSI C, Fourth Edition * Input/Output Library Functions getchar() can be used for single character input int getchar() The reason for returning characters in integer format is to allow the End-Of-File (EOF
显示全部