 We've already talked about modules and importing math earlier in the course. So what I want to start with in this video is the random module. In the shell, let's import random. And then if you use the random dot random function, it will return you a random number between 0 and 1, excluding 1. So let's run it a few times here. And each time we get a brand new number from 0 up to but not including 1. For things like card games, you'd like to be able to generate random numbers in the range 0 to 51, which gives you 52 total. And for that you have random range. If you give it a single number, it generates a number from 0 up to but not including that number. So if I say random dot random range of 52, I get 39, and this time I'm just going to use up arrow to make my life a little bit easier. And each time I run it, I get some different number, and there's no guarantee that I won't get duplicates, because as you can see, I am getting those. If I were programming a dice game, I'd like to have a random number in the range 1 to 6. So I can say random dot random range with a lower bound and an upper bound. This will generate a number from 1 up to but not including 7. So there's a 2, a 3, a 5, and so on. At this point, we can't write any dice or card games, because we're missing the concept of selection, which we're going to see in chapter 7. We can, however, use random numbers to generate random artwork. I'm going to go into a lot of detail about how I wrote and designed this program. And here's where the video may get a bit strange, as I'm going to be alternating between screen recording and a regular camera. Here's what I want the program to do. I want to draw randomly sized triangles, squares, pentagons, and hexagons all centered around the center of the screen. I'm going to take you through the steps I used to create this program, including a mistake or two, because it's important to see the design process in action. Here's the general outline of the program. In lines 4 through 6, I'm going to import all of the modules that I need. In lines 8 and 9, I'm going to set up the turtle. And then starting in line 11, I'm going to draw 10 shapes where I randomly choose the length of each side and the number of sides that I want. And then draw the appropriate polygon. I can fill in part of this right away. The length of the side is going to be some random integer in the range 50 to 200 in steps of 10. My random number of sides is going to be a random number, again an integer, in the range 3 up to but not including 7. Which will give me triangles, squares, pentagons, and hexagons. The problem now is how to draw a centered polygon. I'm starting with the square first. I need to know how far to move from the center of the screen. And then I also need to know how far I'm going to have to turn the turtle, which is facing this direction, so that I'm oriented properly to draw the square. Let's label some of this. Let's call this distance that I have to move x, and this will also be x. Here's my polygon angle, which happens to be 90 degrees, or 360 divided by 4. This means these other two angles, let's call them theta, are 45 degrees, which is half of 90 degrees. Now, how can I find x? One way to do it is to drop a perpendicular to this side, whose length is side, and now I have another right angle triangle. This right angle triangle has a base of length side over 2, and its hypotenuse is x. Some trigonometry will tell me that the cosine of theta is equal to the adjacent side with that length over the length of the hypotenuse, and some algebra tells me that x is going to be side over 2 divided by the cosine of theta. How far do I have to turn to get oriented properly to draw the square? Right now, the turtle is pointing straight north. If I were to turn 180 degrees, I would be facing straight south, and then if I back off by theta degrees, that's the direction I want to point. So the angle that I have to turn by in order to start drawing is 180 minus theta. Now that I have the math arranged, I'm going to temporarily set my program to draw only squares by putting a 4 in there, so I'll always draw a square, and then I'm going to put in the code for drawing the polygon. My polygon angle is going to be 360 divided by the number of sides. That's my interior angle, so to speak. And the theta is going to be my polygon angle divided by 2. The distance I have to move before drawing the polygon is going to be side over 2 divided by the cosine of theta. Theta is in degrees, but cosine wants radians, and that's why I have to do the conversion to radians here. For each shape, I want to return the turtle to its home position. I'll set the pen size to 3 to make it more visible. I'll go left 90 degrees and then forward by that distance that we calculated, and then to get the turtle oriented properly, right 180 minus theta. Drawing the polygon is something we've seen before. For count in the range in the sides, I'm going to go forward by the length of the side, and then right by my polygon angle. Let's run this and see how it works. You'll see that I should lift the pen up when I move away from the center, but this is a good help at least to see that I'm doing the right thing. That's looking good. Now, let's put back the random range so I get a random number of sides and see how that works. Also, by the way, I'm going to set the turtle's speed to zero so that it draws as fast as possible. Oh goodness, that's not what I wanted. I love the abstract look, but the triangles, the pentagons, and the hexagons are not centered as the squares were. So the question is, what's wrong here? This is what happened, by the way, when I was writing the program the first time. I got this result and it took me about 15 minutes to figure out what's going wrong. It's this line right here where I take theta as the polygon angle divided by 2. That's the correct angle for a square, but not for any other figure. Let's take a look at a pentagon. This interior angle here is 72 degrees. It's the polygon angle, 360 divided by 5. And my original formula was to say that theta is equal to the polygon angle divided by 2. So that would make this angle theta 36 degrees. That can't be right, because 72 plus 36 plus 36 doesn't add up to 180. It only adds up to 144. Since the sum has to be 180, the correct formula for theta is that it's 180 minus the polygon angle divided by 2. It was just an unlucky accident that this simplistic formula happens to work out to 45 degrees for the square, 90 divided by 2, and 180 minus 90 divided by 2 also works out to the correct answer of 45, and I mistakenly thought that this was the correct formula. It isn't. This is the one I have to work with. So instead of naively dividing by 2, what I really need to do is calculate the angle correctly, namely 180 minus my polygon angle divided by 2. And once I make that change, the code starts working very nicely, and then the only thing I need to add at the very end is to bring the pen up before I start drawing, and to put the pen down when it's in position. And there's the program that I was looking for. The most important thing to take away from this is that I planned this program out. I drew a picture of what I wanted, and I labeled it. That's not a guarantee that the program will be correct. I found that out when I had derived the wrong formula for theta, but that planning and the labeling and the drawing got me a lot closer to the solution than if I had just sat down and started hammering away at the keyboard.