1 00:00:05,560 --> 00:00:09,320 In this video, we'll learn about what functions are in c++. 2 00:00:10,000 --> 00:00:13,849 When we develop c++ programs, we typically use the predefined 3 00:00:13,860 --> 00:00:17,220 functions and classes in the c++ standard libraries. 4 00:00:17,709 --> 00:00:19,910 These are functions and classes that have already been written 5 00:00:19,910 --> 00:00:22,810 for us and tested, and they're available for us to use. 6 00:00:23,420 --> 00:00:25,540 We've already seen how useful the standard vector and 7 00:00:25,570 --> 00:00:27,400 standard string classes can be. 8 00:00:27,969 --> 00:00:31,040 C++ has many other libraries that we can use in the same way. 9 00:00:31,599 --> 00:00:34,739 Depending on our application, we may also use third-party 10 00:00:34,739 --> 00:00:35,919 functions in libraries. 11 00:00:36,150 --> 00:00:39,460 These may be open source commercial or other types of libraries that 12 00:00:39,460 --> 00:00:41,920 have been developed by other programmers and are available 13 00:00:42,130 --> 00:00:43,730 to use in our applications. 14 00:00:45,340 --> 00:00:48,879 So if we want to develop games in c++, we'd very likely use 15 00:00:48,880 --> 00:00:52,939 third-party libraries or frameworks such as Coco's, 2DX, Unreal, 16 00:00:52,949 --> 00:00:55,129 Spring, Cryengine or others. 17 00:00:55,990 --> 00:00:57,820 Why would we use third-party libraries? 18 00:00:57,830 --> 00:00:58,830 Well, that's pretty easy. 19 00:00:59,040 --> 00:01:01,220 Because these libraries take a tremendous amount 20 00:01:01,220 --> 00:01:02,290 of effort to develop. 21 00:01:02,519 --> 00:01:04,938 And chances are high that the functionality you need 22 00:01:05,120 --> 00:01:07,779 has already been implemented and tested and ready to use. 23 00:01:08,230 --> 00:01:09,570 So why reinvent the wheel? 24 00:01:10,240 --> 00:01:12,550 Well using existing functions and classes from 25 00:01:12,559 --> 00:01:14,080 existing libraries is great. 26 00:01:14,420 --> 00:01:17,670 But even then, you still need to write your own c++ functions and 27 00:01:17,670 --> 00:01:20,539 classes that are specific to the problem you're trying to solve. 28 00:01:21,179 --> 00:01:24,079 We'll talk about classes and object-oriented programming soon. 29 00:01:24,650 --> 00:01:26,710 In this section, we'll focus on functions. 30 00:01:27,219 --> 00:01:29,509 Functions allow us to modularize our program. 31 00:01:30,400 --> 00:01:33,470 This is a big deal since we can divide our program into logical 32 00:01:33,470 --> 00:01:37,039 self-contained units of code that we can reuse whenever we need to. 33 00:01:37,150 --> 00:01:38,419 Let's see a simple example. 34 00:01:40,160 --> 00:01:42,140 On the left hand side of the screen, you can see a program 35 00:01:42,140 --> 00:01:44,310 written in the way we've been writing them up to this point. 36 00:01:44,870 --> 00:01:46,569 Lots of statements one after the other. 37 00:01:46,799 --> 00:01:48,900 Some of these statements might be if statements, some 38 00:01:48,900 --> 00:01:50,139 might be looping statements. 39 00:01:50,390 --> 00:01:54,360 But you can see that this program has three logical sections: one for input, 40 00:01:54,370 --> 00:01:56,340 one for processing and one for output. 41 00:01:56,620 --> 00:01:58,199 But everything's in the main function. 42 00:01:58,840 --> 00:02:02,149 This is okay for relatively small programs like the ones we've written. 43 00:02:02,420 --> 00:02:06,089 But as programs get larger and more complex, this breaks down quickly and 44 00:02:06,090 --> 00:02:07,790 the code becomes difficult to manage. 45 00:02:08,288 --> 00:02:11,410 Also, we tend to copy and paste code a lot since we may 46 00:02:11,410 --> 00:02:14,120 need the same functionality in different places in the program. 47 00:02:14,860 --> 00:02:17,500 There's a field in computer science called software engineering. 48 00:02:17,710 --> 00:02:20,799 Software engineering is all about designing your solutions and writing 49 00:02:20,799 --> 00:02:24,610 your code in a way that can be easily understood, debugged and extended. 50 00:02:25,420 --> 00:02:28,630 One of the fundamental principles in software engineering is abstraction. 51 00:02:29,010 --> 00:02:31,800 Abstraction is exactly what functions help us achieve. 52 00:02:32,160 --> 00:02:34,370 Let's look at the example on the right side of the screen. 53 00:02:34,920 --> 00:02:37,539 In this case, we've replaced ten statements with three. 54 00:02:37,929 --> 00:02:39,900 These three statements are function calls. 55 00:02:40,299 --> 00:02:44,310 Now when we look at our main function, we see less detail, more abstraction. 56 00:02:44,970 --> 00:02:48,530 We can now see read input, process input and provide output. 57 00:02:48,740 --> 00:02:50,570 So we've modularized our program. 58 00:02:50,990 --> 00:02:53,700 We know that we still have to execute all the code we had before. 59 00:02:54,020 --> 00:02:56,390 But we've removed it from main and replaced it with a call 60 00:02:56,390 --> 00:02:57,790 to functions in each case. 61 00:02:58,520 --> 00:02:59,950 Let's see what these functions look like. 62 00:03:01,250 --> 00:03:03,650 On the left side of the screen, we see the same main function 63 00:03:03,650 --> 00:03:04,870 with the three function calls. 64 00:03:05,210 --> 00:03:07,800 And on the right side of the screen, we see the implemented 65 00:03:07,800 --> 00:03:10,870 functions: read input, process input and provide output. 66 00:03:11,540 --> 00:03:13,980 Notice that each of these functions executes the same 67 00:03:13,980 --> 00:03:16,880 statements that we were executing previously in the main function. 68 00:03:17,650 --> 00:03:20,676 This makes our code more modular, more readable, more maintainable, 69 00:03:20,690 --> 00:03:22,489 easier to debug and easier to reuse. 70 00:03:23,290 --> 00:03:25,270 Let's see how this can help us develop programs. 71 00:03:26,790 --> 00:03:29,509 A common analogy that's used when learning about functions 72 00:03:29,550 --> 00:03:31,360 is the boss/worker analogy. 73 00:03:32,040 --> 00:03:34,500 Let's assume you're the boss, and you need to get a task done. 74 00:03:34,920 --> 00:03:38,650 This task involves several subtasks, and your job is not to perform 75 00:03:38,650 --> 00:03:42,040 the individual subtasks but to coordinate the execution of those 76 00:03:42,050 --> 00:03:45,770 subtasks and report the synthesized results back to your own boss. 77 00:03:46,210 --> 00:03:48,559 So you've hired three workers that have expertise 78 00:03:48,559 --> 00:03:50,160 in each of those subtasks. 79 00:03:50,710 --> 00:03:54,069 So you assign a subtask to each worker and provide each of them whatever 80 00:03:54,110 --> 00:03:55,990 information they need to perform it. 81 00:03:56,530 --> 00:03:58,980 Since each of the workers knows exactly what to do. 82 00:03:59,210 --> 00:04:02,389 When they complete their subtask, they report their results back to you. 83 00:04:02,790 --> 00:04:06,830 This may simply be a comment that says I'm done or could be a complex report 84 00:04:06,830 --> 00:04:08,390 that they created and provide to you. 85 00:04:09,190 --> 00:04:11,580 Now that all subtasks are completed, you look at the 86 00:04:11,580 --> 00:04:14,570 individual results, synthesize the report and give it to your boss. 87 00:04:15,380 --> 00:04:17,360 Sounds like an easy job being a boss, right. 88 00:04:17,720 --> 00:04:18,500 Actually, no. 89 00:04:18,589 --> 00:04:21,689 The boss needs to know what worker to assign the specific subtask 90 00:04:21,709 --> 00:04:25,320 to, the order to do them in, the information to provide to each 91 00:04:25,320 --> 00:04:26,719 of them, so they can do their job. 92 00:04:27,179 --> 00:04:30,069 And the boss needs to know what he or she is getting back in order 93 00:04:30,070 --> 00:04:31,970 to synthesize it and report back. 94 00:04:32,700 --> 00:04:34,790 This is a great analogy to using functions. 95 00:04:35,270 --> 00:04:37,570 If you need to call functions, you write your code to the 96 00:04:37,570 --> 00:04:39,010 function specification, 97 00:04:39,520 --> 00:04:41,329 you need to understand what the function does. 98 00:04:41,509 --> 00:04:43,969 You need to understand what information the function needs. 99 00:04:44,219 --> 00:04:46,500 You need to understand what the function returns to you. 100 00:04:46,840 --> 00:04:49,890 You need to understand any errors that the function may produce and why. 101 00:04:50,410 --> 00:04:53,300 You need to understand any performance constraints and so on. 102 00:04:53,960 --> 00:04:56,569 What you don't need to worry about is how the function works 103 00:04:56,599 --> 00:05:00,390 internally unless of course you're the programmer coding the function. 104 00:05:01,099 --> 00:05:03,020 This is super important to understand. 105 00:05:03,410 --> 00:05:06,339 When we use string, vectors, math functions, complex 106 00:05:06,359 --> 00:05:08,039 image processing libraries. 107 00:05:08,340 --> 00:05:11,150 Collision, detection and games, we don't need to know 108 00:05:11,160 --> 00:05:12,710 how those algorithms work. 109 00:05:12,820 --> 00:05:15,159 This concept is called information hiding. 110 00:05:15,469 --> 00:05:18,820 And we'll revisit this again when we talk about object-oriented classes. 111 00:05:19,200 --> 00:05:20,440 Let's see an example of this. 112 00:05:21,320 --> 00:05:23,400 C++ has many libraries. 113 00:05:23,469 --> 00:05:25,560 One library is a mathematics library. 114 00:05:25,940 --> 00:05:29,020 In order to use this library, we must include cmath. 115 00:05:29,830 --> 00:05:32,470 The cmath library has many mathematical functions, 116 00:05:32,470 --> 00:05:36,030 including trig, exponential logarithmic power, rounding and 117 00:05:36,030 --> 00:05:37,340 many more kinds of functions. 118 00:05:37,840 --> 00:05:40,399 These functions are global functions, which means they're 119 00:05:40,500 --> 00:05:42,129 available to the entire program. 120 00:05:42,270 --> 00:05:44,640 We call these functions by using their name, followed 121 00:05:44,640 --> 00:05:45,979 by a series of arguments. 122 00:05:46,260 --> 00:05:48,740 In the output statement on this slide, we're calling square root 123 00:05:48,780 --> 00:05:51,250 and we're passing in 400.0 to it. 124 00:05:51,500 --> 00:05:54,349 The square root function will calculate the square root of 400 and 125 00:05:54,349 --> 00:05:55,850 return the result which is printed. 126 00:05:56,480 --> 00:05:59,369 In the last example, we're calling the power, power function, 127 00:05:59,609 --> 00:06:01,200 which expects two arguments. 128 00:06:01,550 --> 00:06:05,550 In this case we're calculating 2.0 raised to the 3.0 power. 129 00:06:05,900 --> 00:06:08,600 Notice that to use these functions, all we need to know is what they 130 00:06:08,600 --> 00:06:12,450 do, their name, what they expect from us and what they return to us. 131 00:06:12,889 --> 00:06:16,449 How the square root or the power is being calculated isn't something 132 00:06:16,449 --> 00:06:17,720 I need to know to use them. 133 00:06:18,039 --> 00:06:19,790 That's classic abstraction. 134 00:06:20,500 --> 00:06:23,560 There are many built-in functions and classes in c++. 135 00:06:23,849 --> 00:06:26,240 However, we'll need to learn to design and write our own. 136 00:06:26,670 --> 00:06:29,109 In this section, we'll learn to write our own functions. 137 00:06:29,480 --> 00:06:31,790 Here's a preview of a very simple function so you 138 00:06:31,790 --> 00:06:32,740 can see where we're going. 139 00:06:33,240 --> 00:06:35,620 Don't worry about the syntax I'll cover all that later. 140 00:06:36,320 --> 00:06:39,039 In this case, I have a function called add numbers. 141 00:06:39,310 --> 00:06:41,510 And it expects two integers named a and b. 142 00:06:42,250 --> 00:06:45,320 The body of the function simply returns a plus b. 143 00:06:45,639 --> 00:06:49,389 So we take the sum of a plus b and return the result to the caller. 144 00:06:50,070 --> 00:06:53,090 Notice the int to the left of the function name. That tells 145 00:06:53,090 --> 00:06:54,650 you what the function returns. 146 00:06:54,850 --> 00:06:56,050 In this case, an integer. 147 00:06:56,620 --> 00:06:57,460 Pretty easy, right? 148 00:06:57,740 --> 00:07:00,840 Now every time we need to add two integers, we can call add numbers 149 00:07:00,880 --> 00:07:03,650 and supply the two arguments that the function expects. 150 00:07:04,460 --> 00:07:06,680 Then I can do whatever i want with the result I get back. 151 00:07:06,740 --> 00:07:07,989 In this case, I'm displaying it. 152 00:07:08,320 --> 00:07:09,729 I know what you're probably thinking. 153 00:07:09,900 --> 00:07:13,000 Why in the world would I write a function that adds two numbers when 154 00:07:13,000 --> 00:07:14,560 I can just use the plus operator. 155 00:07:15,070 --> 00:07:16,140 Well, normally you wouldn't. 156 00:07:16,340 --> 00:07:19,070 But suppose we had a requirement that if any of those numbers is 157 00:07:19,090 --> 00:07:21,490 negative, then we must return 0. 158 00:07:21,800 --> 00:07:23,500 Let's see how we can modify this function. 159 00:07:24,700 --> 00:07:27,570 So this is what the function might look like if we had to return 160 00:07:27,730 --> 00:07:29,799 0 if either a or b is negative. 161 00:07:30,270 --> 00:07:32,770 It would probably be a good idea to give this function a better name. 162 00:07:32,900 --> 00:07:36,150 But hopefully, this preview example gives you a good high level view 163 00:07:36,150 --> 00:07:38,099 of a simple user defined function. 164 00:07:38,559 --> 00:07:41,340 Let's head over to the IDE and use some of the functions 165 00:07:41,340 --> 00:07:43,050 in the c++ standard library. 166 00:07:44,280 --> 00:07:48,310 Before we learn to write and define our own functions, it's important 167 00:07:48,320 --> 00:07:51,270 to understand to use functions that are already defined for us. 168 00:07:51,950 --> 00:07:55,330 I've opened up a browser, and I've gone to cppreference.com. 169 00:07:55,630 --> 00:07:59,030 I'm going to show you three pages within this website, and I'll provide 170 00:07:59,040 --> 00:08:01,079 urls and the resources for the course. 171 00:08:01,910 --> 00:08:04,729 Here, I'm going to the c++ standard library header files. 172 00:08:04,730 --> 00:08:07,359 Here are all the standard header files in c++. 173 00:08:08,010 --> 00:08:10,000 And you can see, there's a whole bunch of them. 174 00:08:10,350 --> 00:08:13,360 This would become your next learning experience in c++. 175 00:08:14,240 --> 00:08:17,639 Once you understand the basics of the programming language, once you 176 00:08:17,639 --> 00:08:21,320 understand object orientation within c++, then it's a matter of learning 177 00:08:21,320 --> 00:08:23,120 what's out there and available to you. 178 00:08:23,709 --> 00:08:26,169 There's a lot of stuff out there, and there's a lot to learn. 179 00:08:26,170 --> 00:08:28,370 But don't worry you won't learn it all at once, you'll learn it 180 00:08:28,370 --> 00:08:30,620 little by little as you use c++. 181 00:08:31,030 --> 00:08:33,890 What I want to do here is I want to talk about a couple of libraries, 182 00:08:34,080 --> 00:08:36,969 and we'll run a couple of example programs using those libraries. 183 00:08:36,969 --> 00:08:39,049 We'll use some of the functions in those libraries. 184 00:08:40,000 --> 00:08:42,460 The first library we'll use is the math library. 185 00:08:42,470 --> 00:08:46,830 And I've got a pane open right here to the cmath standard library header. 186 00:08:47,500 --> 00:08:49,560 Now we're going to use some of the functions in here, you can 187 00:08:49,560 --> 00:08:51,120 see that they're all defined. 188 00:08:51,130 --> 00:08:53,599 And if you need information about any of them, you can just click on 189 00:08:53,599 --> 00:08:55,270 here, and you can learn about them. 190 00:08:55,639 --> 00:08:58,730 We're going to use the square root, the cube root and the power function. 191 00:08:59,600 --> 00:09:01,199 They're just like what you would think, right. 192 00:09:01,200 --> 00:09:02,810 You pass in a number, you get the square root. 193 00:09:02,850 --> 00:09:04,470 You pass in a number, you get the cube root. 194 00:09:04,830 --> 00:09:08,270 And in this case, I pass in two numbers to the power function, and it 195 00:09:08,270 --> 00:09:10,350 will raise the base to some exponent. 196 00:09:10,730 --> 00:09:13,450 And here's a whole bunch of information about those functions. 197 00:09:13,809 --> 00:09:17,530 Okay, we're also going to do sin and cosin and a couple of other 198 00:09:17,530 --> 00:09:21,390 interesting and very useful functions, the ceiling, the floor and round. 199 00:09:21,670 --> 00:09:24,739 The ceiling of a floating point number is the next integer. 200 00:09:24,750 --> 00:09:27,810 So if my number is 12.4, the ceiling would be 13. 201 00:09:29,030 --> 00:09:31,040 The floor is the other way around. 202 00:09:31,130 --> 00:09:33,909 So if I have 12.4, then the 4 will be 12. 203 00:09:34,660 --> 00:09:37,870 And rounding works just like you would expect, 12.5 rounds up 204 00:09:37,870 --> 00:09:41,180 to 13, 12.4 rounds down to 12. 205 00:09:41,270 --> 00:09:42,400 So that's what we'll do. 206 00:09:42,410 --> 00:09:44,500 And then in the next little program we'll write, we'll 207 00:09:44,500 --> 00:09:46,060 come to the c standard library. 208 00:09:46,060 --> 00:09:48,069 And I'll open up that website for you as well. 209 00:09:48,590 --> 00:09:50,160 This is c standard lib. 210 00:09:50,200 --> 00:09:51,520 That's the name of the library header. 211 00:09:51,520 --> 00:09:52,590 That's what you need to include. 212 00:09:52,590 --> 00:09:54,800 And there's a couple of functions in here that we'll use. 213 00:09:54,820 --> 00:09:57,969 And I'll scroll down just a little bit there right here, it's rand and srand. 214 00:09:59,290 --> 00:10:02,440 Rand generates a random number, a pseudo-random number. 215 00:10:02,440 --> 00:10:04,199 And I'll explain what that means in a little bit. 216 00:10:04,530 --> 00:10:06,990 And srand seeds in the random number generator. 217 00:10:06,990 --> 00:10:10,790 So what we'll do is, we'll use this function, rand, to generate 218 00:10:10,809 --> 00:10:13,090 the num -- let's say we're rolling a 6-sided dice, and we can 219 00:10:13,090 --> 00:10:15,400 get something between 1 and 6. 220 00:10:15,410 --> 00:10:17,480 We could really make that between anything and anything, and 221 00:10:17,480 --> 00:10:18,660 I'll show you how to do that. 222 00:10:18,740 --> 00:10:21,750 And then we'll see that random number generator so that the patterns of 223 00:10:21,750 --> 00:10:23,830 numbers we get will differ every time. 224 00:10:23,940 --> 00:10:26,409 So I'm going to head over to the IDE now, and we'll start 225 00:10:26,410 --> 00:10:27,460 with the math functions. 226 00:10:29,389 --> 00:10:30,439 Okay, so I'm back in the IDE. 227 00:10:30,890 --> 00:10:35,030 And I'm in the section 11 workspace in the math project. 228 00:10:35,920 --> 00:10:38,569 And here we're just doing some real simple math examples to show 229 00:10:38,570 --> 00:10:40,050 you how to use these functions. 230 00:10:40,790 --> 00:10:43,450 First of all, we're including cmath that's required. 231 00:10:43,450 --> 00:10:45,710 Otherwise, you're going to get a lot of compiler errors because 232 00:10:45,710 --> 00:10:47,680 the compiler won't know about square root, it won't know 233 00:10:47,680 --> 00:10:49,140 about sign or anything else. 234 00:10:49,460 --> 00:10:53,630 In this little example, I'm declaring a double called num, and I'm asking 235 00:10:53,630 --> 00:10:55,170 the user to enter that number. 236 00:10:55,620 --> 00:10:58,500 Once the user enters the number, I'm simply calling the square root 237 00:10:58,500 --> 00:11:01,230 function that will return the square root of the number, and we'll display 238 00:11:01,230 --> 00:11:05,330 it right in line the cube root, the sin, the cosin, the ceiling, the 239 00:11:05,330 --> 00:11:06,860 floor, and we'll round that number. 240 00:11:07,280 --> 00:11:09,710 Then after that, I'll ask the user for another number, which 241 00:11:09,710 --> 00:11:11,150 I've defined called power. 242 00:11:11,300 --> 00:11:14,960 In this case, we'll raise that number that they typed into this power. 243 00:11:14,960 --> 00:11:18,370 So if they typed in 2.5, we'll raise it to whatever power they wanted to. 244 00:11:18,520 --> 00:11:20,920 And again here what we're doing is we're calling the power function 245 00:11:20,920 --> 00:11:24,760 right here, and we're passing in the number and the power to raise it to. 246 00:11:25,240 --> 00:11:26,240 Pretty simple. 247 00:11:26,310 --> 00:11:29,959 You can see from this code, I don't know how square root works or cube 248 00:11:29,960 --> 00:11:32,230 root works or sin or cosin or power. 249 00:11:32,290 --> 00:11:34,830 I don't know how this stuff is implemented behind the scenes. 250 00:11:35,110 --> 00:11:35,970 That's the whole point. 251 00:11:36,300 --> 00:11:37,839 I don't care, I don't need to know. 252 00:11:38,009 --> 00:11:40,859 And this is where abstraction really shines because we're 253 00:11:40,859 --> 00:11:42,170 just trying to solve a problem. 254 00:11:42,170 --> 00:11:44,170 And I know I need to raise something to a power. 255 00:11:44,410 --> 00:11:46,340 Well, I know I need a sin or cosin. 256 00:11:46,340 --> 00:11:50,120 I can simply look up the library, understand how that function is 257 00:11:50,120 --> 00:11:52,740 being called, understand what that function returns and then 258 00:11:52,740 --> 00:11:54,140 just use it and use the result. 259 00:11:54,170 --> 00:12:00,229 So let's run this program, and let's say the number we enter is 125.6. 260 00:12:00,529 --> 00:12:01,430 And I'll press enter. 261 00:12:02,290 --> 00:12:06,150 So the square root of 125.6 is 11.2071. 262 00:12:06,650 --> 00:12:08,930 The cube root we expect to be right around 5, right. 263 00:12:08,930 --> 00:12:13,780 Because 5 times 5 is 25 times 5 is 125 so that gives us a -- it's not a 264 00:12:13,780 --> 00:12:17,029 formal test case, but it's a pretty informal test case that makes us 265 00:12:17,029 --> 00:12:19,000 feel good that our code is working. 266 00:12:19,680 --> 00:12:22,020 Here's the sin of the number, the cosin of the number, now 267 00:12:22,030 --> 00:12:26,890 the ceiling of 125.6 is the next integer up which is 126. 268 00:12:27,410 --> 00:12:30,240 The floor would be the integer down, which is 125. 269 00:12:30,660 --> 00:12:34,960 And in this case, we round 125.6 up to 126. 270 00:12:35,109 --> 00:12:36,159 So let's enter a power. 271 00:12:36,160 --> 00:12:38,780 Let's raise this to the 4.2 power. 272 00:12:40,260 --> 00:12:41,849 There you go. That's a pretty big number. 273 00:12:42,020 --> 00:12:48,880 So 125.6 raised to the 4.2 power is 6.54269 times 10 to the 8th. 274 00:12:49,280 --> 00:12:50,130 There you go. 275 00:12:50,200 --> 00:12:53,229 We've used the math functions without knowing anything 276 00:12:53,230 --> 00:12:54,409 about how they're implemented. 277 00:12:54,700 --> 00:12:57,450 That's a real, real powerful place to be. 278 00:12:57,740 --> 00:13:01,480 Let me go back to the IDE now, and we'll create some random numbers. 279 00:13:03,510 --> 00:13:07,109 Okay, so I'm back in the IDE, and I'm still in the section 11 workspace. 280 00:13:07,309 --> 00:13:09,290 This time I'm in the random project. 281 00:13:09,759 --> 00:13:13,610 Now in order to use random numbers, I'm using the rand function. 282 00:13:13,620 --> 00:13:17,920 Now the rand function has been used a long time in c++ and in c as well. 283 00:13:18,380 --> 00:13:21,480 But there's a better way to create random numbers if you're really 284 00:13:21,480 --> 00:13:24,920 doing statistical processing and that's the random header file. 285 00:13:25,370 --> 00:13:26,250 I'm not going to cover that. 286 00:13:26,250 --> 00:13:28,490 I'm just going to use rand since that's what we're learning 287 00:13:28,490 --> 00:13:29,589 about calling functions. 288 00:13:29,590 --> 00:13:33,600 But if you really need very precise distributions, consider 289 00:13:33,600 --> 00:13:35,200 using the random header file. 290 00:13:35,340 --> 00:13:38,140 Okay, so in this case, we're including the c standard library 291 00:13:38,440 --> 00:13:40,479 that gives us the rand function. 292 00:13:41,050 --> 00:13:44,420 And we're also including the c time library. 293 00:13:44,650 --> 00:13:46,050 I'll talk about that in a minute. 294 00:13:46,180 --> 00:13:49,670 In order to use random numbers, we really need to seed a 295 00:13:49,670 --> 00:13:50,940 random number generator. 296 00:13:50,940 --> 00:13:52,570 Now what's a random number generator? 297 00:13:52,570 --> 00:13:56,120 A random number generator is just a generator that creates random numbers. 298 00:13:56,330 --> 00:13:58,710 Computers are real bad with random numbers. 299 00:13:59,130 --> 00:14:01,139 We have what are called pseudorandom numbers. 300 00:14:01,139 --> 00:14:05,160 What that means is that the numbers themselves are random but the sequence 301 00:14:05,160 --> 00:14:07,010 that they're generated in is not. 302 00:14:07,010 --> 00:14:11,270 So in order to create more real random numbers, we want to seed that random 303 00:14:11,270 --> 00:14:14,799 number generator with a different value each time that way it creates 304 00:14:14,800 --> 00:14:16,620 different sequences of values. 305 00:14:16,800 --> 00:14:17,919 So we'll do that in a second. 306 00:14:17,920 --> 00:14:20,589 I'll do it without seeding and with seeding so you can see the difference. 307 00:14:20,900 --> 00:14:22,440 But let's talk about what's going on here. 308 00:14:22,539 --> 00:14:24,360 I need a random number generated, right. 309 00:14:24,360 --> 00:14:26,680 So I'm going to create a variable called random number 310 00:14:26,680 --> 00:14:28,140 and I'm initializing it to 0. 311 00:14:28,140 --> 00:14:30,240 And then what I want to do is, I just want to loop and 312 00:14:30,240 --> 00:14:31,710 create 10 random numbers. 313 00:14:32,210 --> 00:14:34,159 And that's what my count variable is. 314 00:14:34,330 --> 00:14:36,250 I can make that as whatever I like. 315 00:14:36,250 --> 00:14:37,449 But in this case, it's a 10. 316 00:14:38,099 --> 00:14:40,990 And then in this case, I'm assuming we're rolling a die. 317 00:14:41,000 --> 00:14:45,210 So I want to return a random number between 1 and 6 inclusive. 318 00:14:45,300 --> 00:14:48,870 So 1 2 3 4 5 or 6. 319 00:14:49,010 --> 00:14:50,740 This could be 1 to 10. This could be 1 and 2 320 00:14:50,740 --> 00:14:51,800 if you're flipping a coin. 321 00:14:52,040 --> 00:14:54,560 This could be anything you like, whatever range you like. 322 00:14:54,799 --> 00:14:57,109 This is how you would create that range. 323 00:14:57,120 --> 00:14:59,529 And I'm just going to create min and max to be 1 and 6. 324 00:14:59,590 --> 00:15:02,260 You can modify those to whatever you like to create 325 00:15:02,260 --> 00:15:03,439 whatever range you need. 326 00:15:03,780 --> 00:15:06,150 Okay, now the rand. 327 00:15:06,150 --> 00:15:08,469 If you look at the rand function in the documentation, it 328 00:15:08,470 --> 00:15:13,620 says that it returns a random number between 0 and rand max. 329 00:15:14,110 --> 00:15:16,699 I have no idea what rand max is on my system. 330 00:15:16,699 --> 00:15:18,380 It'll probably be different on your system. 331 00:15:18,670 --> 00:15:20,220 But it's guaranteed to be at least 32767. 332 00:15:22,230 --> 00:15:24,040 So I'll run it here, and we'll see what we get. 333 00:15:24,500 --> 00:15:26,740 Now this function right here is the one that's seeding 334 00:15:26,740 --> 00:15:28,180 the random number generator. 335 00:15:28,760 --> 00:15:31,170 Remember, we want to seed this random number generator 336 00:15:31,170 --> 00:15:33,850 with a different number every time that we run this program. 337 00:15:34,100 --> 00:15:36,660 So what better way to seed it than with the system time. 338 00:15:36,920 --> 00:15:38,420 And that's what's going on right here. 339 00:15:38,670 --> 00:15:41,959 I'm seeding the random number generator to -- I'm calling 340 00:15:41,960 --> 00:15:44,740 the time function and passing in a null pointer which is 0. 341 00:15:45,110 --> 00:15:48,575 This line of code right here you can see an example of that in the 342 00:15:48,575 --> 00:15:51,440 docs that I showed you earlier, it shows you exactly how to seed 343 00:15:51,440 --> 00:15:52,600 the random number generator. 344 00:15:52,830 --> 00:15:55,050 So now we've seeded the random number generator. 345 00:15:55,410 --> 00:15:59,380 And what we want to do is we want to generate 10 random numbers. 346 00:15:59,860 --> 00:16:04,150 So I'm going to start from I equals 1, while I is less than or equal to count. 347 00:16:04,160 --> 00:16:07,889 So I'm going from 1 to 10 by 1, there's my increment. 348 00:16:08,509 --> 00:16:11,990 And then each iteration of the loop, I'm going to call rand. 349 00:16:12,670 --> 00:16:17,750 Rand returns a number between 0 and 32767 or higher in this case. 350 00:16:17,810 --> 00:16:19,890 I have no idea what rand max is on my system. 351 00:16:20,110 --> 00:16:21,040 We'll see in a minute. 352 00:16:21,139 --> 00:16:23,169 I need to really scale that number down. 353 00:16:23,179 --> 00:16:25,090 Because I only want numbers between 1 and 6. 354 00:16:25,389 --> 00:16:30,140 So if I take rand and I take the modulus of it, based on max, 355 00:16:30,140 --> 00:16:34,329 right, so take rand divide it by 6 and whatever the remainder is, 356 00:16:34,690 --> 00:16:36,459 I've scaled that number down now. 357 00:16:36,740 --> 00:16:39,670 So in this case, if I divide a number by 6, the remainder's either 358 00:16:39,670 --> 00:16:42,270 going to be 0 1 2 3 4 or 5, right. 359 00:16:42,320 --> 00:16:43,839 Those are the only options I have. 360 00:16:44,200 --> 00:16:48,820 But since I want to go 1 to 6, I just add min into it, and it's as simple 361 00:16:48,820 --> 00:16:53,189 as that, so that's going to give me a number 1 through 6 inclusive. 362 00:16:53,890 --> 00:16:55,140 And I'm going to output it. 363 00:16:55,490 --> 00:16:58,010 So let's run this program and see how it runs here. 364 00:16:58,340 --> 00:16:59,190 I'll build and run. 365 00:16:59,940 --> 00:17:01,809 And rand max on my system is 32767. 366 00:17:03,120 --> 00:17:04,430 So now we know what it is. 367 00:17:04,618 --> 00:17:07,240 And here you can see that I'm generating 10 random 368 00:17:07,240 --> 00:17:09,189 numbers 1 through 6. 369 00:17:09,189 --> 00:17:11,980 So I'm getting two 2s, a 5, a 3, a 1, a 2 and so forth. 370 00:17:12,240 --> 00:17:15,480 If I run this, remember the, the first sequence there is 2 2 5. 371 00:17:15,750 --> 00:17:20,839 So let's run this again and now I've got 4 6 3 as the first three numbers. 372 00:17:20,858 --> 00:17:21,980 And I'll run this again. 373 00:17:22,589 --> 00:17:24,618 And now I've got 2 2 1 and so forth. 374 00:17:24,618 --> 00:17:27,940 Now if we did not see that random number generator, let me 375 00:17:28,040 --> 00:17:29,920 comment that line out right here. 376 00:17:30,430 --> 00:17:35,169 If I don't see that and run it again, you can see 66556. 377 00:17:35,230 --> 00:17:38,899 Okay, let me run it again, 66556. 378 00:17:40,459 --> 00:17:43,330 So we see the numbers are random but the sequence 379 00:17:43,330 --> 00:17:44,230 that we're getting is not. 380 00:17:44,230 --> 00:17:46,820 We're always going to get the same sequence of numbers. 381 00:17:46,820 --> 00:17:49,270 Now that could be useful sometimes if you're trying to repeat 382 00:17:49,270 --> 00:17:51,869 experiments and so forth, but usually you don't want that. 383 00:17:52,150 --> 00:17:54,750 And that's why we see the random number generator here. 384 00:17:54,980 --> 00:17:55,550 So there you go. 385 00:17:55,550 --> 00:17:59,099 You see a couple of examples of using some built-in functions we use the 386 00:17:59,100 --> 00:18:03,160 math library and we're using the random number generation library, 387 00:18:03,370 --> 00:18:07,090 and we're using the c standard library to generate random numbers. 388 00:18:07,620 --> 00:18:10,319 None of this do we really care about how the random number 389 00:18:10,320 --> 00:18:13,980 generator works unless that's what really interests you and you can 390 00:18:14,259 --> 00:18:18,050 dig into the libraries and see exactly what algorithms they use. 391 00:18:18,090 --> 00:18:21,280 But if I need to create random numbers, this is how i do it. 392 00:18:21,380 --> 00:18:22,380 Really easy.