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 - C Tutorial
From the course: C: Data Structures, Pointers, and File Systems
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…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
-
-
Solving math puzzles3m 32s
-
Creating random numbers3m 42s
-
Working with time functions3m 54s
-
Sorting data4m 8s
-
Debugging your code2m
-
Using a debugger4m 53s
-
Writing your own header file2m 53s
-
Mixing multiple source code files4m
-
Challenge: Lotto simulation1m 18s
-
Solution: Lotto simulation2m 29s
-
-