C ++ library functions serve as an efficient. NET programming language. It combines the function language and the object-oriented programming language, and is perfect for programming, algorithms, technology, and exploratory development. Therefore, it can be interesting and attractive in the Process of use.
Rand () is used to generate a "pseudo-random number". If the caller does not specify the number x, the default value is used.) a fixed formula 1 is used to return a number y, this number y is a pseudo-random number. At the same time, the fixed formula 2 is used to modify x, so that a new y will be generated when rand () is called next time. Through the above two steps, calling rand () repeatedly produces a so-called "pseudo-random number" sequence.
Because the two calculation formulas are fixed, as long as X1 is the same, the generated sequence is the same. X1 becomes the seed.
If the caller does not specify a seed, the rand () in C ++ uses the default value. Set the seed to use the srand (unsigned int) function. The first letter s in the name of this function indicates seed.
Because of the uniqueness and uniqueness of time, the system time can be used as the seed. This ensures that the sequence generated by rand () is different during each operation. Before using rand (), set the seed as follows,
Srand (unsigned int) time (0 ));
The following is an implementation of rand () and srand () given in C Programming Language, which may be the implementation of early C ++ library functions.
- Int rand (void ){
- NextNext= Next * 1103515245 + 12345; // modify the value of next
- Return (unsigned int) (next/65536) % 32768; // obtain a pseudo-random number ranging from 0 ~ Between 32768
- }
-
- Void srand (unsigned int seed ){
- Next=Seed; // Set the seed
- }
That is to say, if you pick up any C ++ book, including many popular "must-read Classics", it is very likely that the content in this book is not what you should learn, you should not learn it. I say this for two reasons, because I used to be a victim. Second, it is also a more substantive reason, these so-called must-read Classics.
It is filled with the introduction of the traps in C ++ and the various workarounds for the defects of C ++ library functions. It is nice to say that Idioms is used frequently) or techniques technology )); this kind of traps and defects in C ++ are countless, so a long tail is pulled out "; such books have "C defects and traps", "objective Java", and "Objective C #" in all languages ).
- Several minutes to teach you how to use Visual C ++ 6.0 Design programs
- A brief description of the harmonious C ++ code
- In-depth demonstration of high security of C ++
- Description of C ++ unit test instructions
- C ++ standard library description
However, in C ++ library functions, this tail is particularly long, resulting in countless books. 3. The defects and traps listed in these books do not distinguish between common levels. For a programmer, you should want to see the order from the most common problems to the least common problems to list the content. However, these books are either mixed together.
Either introduce the content according to technical categories such as "Resource Management, class design, and generics", which is useless if I see the content of a chapter, of course, do I know whether it is about class design or resource management ?), This makes it impossible for a learner to identify and spend the most important time on the most common problems.