From the course: Complete Guide to C Programming Foundations

Unlock the full course today

Join today to access over 24,800 courses taught by industry experts.

Creating random numbers

Creating random numbers

- [Instructor] Whether you need a value for a simple computer game or to use for testing a function, random numbers are part of computer programming. To generate a random number, you use the rand function shown here at line 10. It's prototyped in the stdLib H header file which is included here. The rand function returns an integer value which is output 10 times in this code. Run to view 10 random values. And there they are. Most programmers want the random numbers to be within a certain range. To clip them, you can use the modulus operator. I'll append the modulus operator here at the end of the rand statement. The mod 100 part of the expression caps the value assigned to variable r in the range of zero to 99. Run to view the clipped random numbers. And there they are. If you don't want the random numbers to include zero, you have to modify the equation. I generally type + 1 here, though you could just put 101. The goal is to keep the range from one through the value specified, which…

Contents