A Random Number Function and a Static Variable

Now that you have some background on the different storage classes, let's look at a couple of programs that use some of them. First, let's look at a function that makes use of a static variable with internal linkage: a random number function. The ANSI C library provides the rand()function to generate random numbers. There are a variety of algorithms for generating random numbers, and ANSI C enables implementations to use the best algorithm for a particular machine. However, the ANSI C standard also supplies a standard, portable algorithm that produces the same random numbers on different systems. Actually, rand() is a “pseudorandom number generator,” meaning that the actual sequence of numbers is ...

Get C Primer Plus, Fourth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.