The rand() function returns a pseudo-random integer value. Programmers often use the modulus operator to clip the random number generated to the desired range. The srand() function seeds the randomizer, providing for different sets of random numbers. Often the output from the time() function is used to seed the randomizer.
- [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. …
- Working with arrays
- Building a structure
- Creating an array of structures
- Testing characters
- Working with strings in C
- Using pointers to manipulate data
- Manipulating files
- Using command-line arguments
- Working with time functions
Skill Level Intermediate
1. Arrays and Structures
2. Characters and Strings
4. Files and the Operating System
5. The Interesting and Extraordinary
Next steps1m 52s
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.