From the course: C: Data Structures, Pointers, and File Systems

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Creating random numbers

Creating random numbers - C Tutorial

From the course: C: Data Structures, Pointers, and File Systems

Start my 1-month free trial

Creating random numbers

- [Instructor] Random numbers may seem well, random. But they're truly the basis for all computer games, even those beyond the simple guess the number variety. To generate a random number you use the rand function shown here at line 10. It's prototyped in the standard library header file which is included at line two. Now the rand function returns, doesn't take any arguments, but it returns an integer value which is output 10 times in this code. And there you see the 10 random integer values. Now most programmers want to ensure that the random value is in a certain range. So to clip them, you use the modulus operator, as shown is this code at line 10. The mod 100 part of the expression caps the value assigned to variable R. And I've modified the loop to output 100 items so let's see how that runs. And there you see 100 random numbers in the range of zero to 99. Now if you don't want the value to include zero, you want it to be one to 100, here's what I do. I add plus one to the…

Contents