 As always this video is a part of a series. There should be an annotation on the screen to the full playlist. I recommend watching the previous videos from this playlist because we're adding on to what we've already learned. This script right here, although I have renamed it randomplacement.html and this when I'm done with this tutorial I will upload this to my website and there will be a link in the description of this video to the files from this series. We have last week's script where we generated 10 cubes lined up as so. Now first things first you see when I rotate and the cubes get far away and again I'm rotating the camera here. If I zoom the camera out you can see the objects getting clipped and again that's because of the clipping we set for the camera when we create it so let's go into our script here go up to where we created the camera and right here we have our near and far clipping let's set this to 10,000 instead of a thousand we'll save that refresh and now you can see I can get the cubes much further away before they start clipping. Clipping is a good thing if you have a lot of stuff going on your scene and stuff in the distance does not really make a difference because clipping will help eliminate stuff that needs to be rendered and help with system resources as your scenes get more complex. So last time we created a variable called mx and said it's negative 500 and then each time we looped we set the x and z position of our object of our mesh to the value of mx but then we added 110 units to mx each time. This time we're going to randomly place our object so I'm going to delete that variable up there because I no longer need it. I'm also going to delete this line where I add to it because again we're not using mx and I'm going to copy and paste this line and change the x here to y so we have x y and z left and right forward and back up and down and what we're going to do here is we're set random position now we could say math dot random and in fact we are going to use that but random through JavaScript gives us a number that is a decimal from anywhere from zero to one so at the very most random will give us the highest number of one and so if I save this and run this we're back to looking again like we only have one cube but actually there's ten cubes that are very close in space if we zoom in here you can see there's multiple cubes overlapping there because this cube is a hundred by a hundred by a hundred in size and so when we're moving from using the random number we're moving the next box randomly at the very most one percent of the size of the box is so it's a very small amount so what we're going to do is well we can multiply to that number but well let's do that let's say and this will work all depending on what you want to do I do recommend downloading these scripts playing with the numbers and seeing the different results so did that now I hit a five and you can see we do have randomly generated those ten boxes but you'll also notice that they're all over to the right and up so there's blank areas here here and here because we're working with completely positive numbers so if this is our our lines of zero and zero we're not gonna have anything on this side of the line anything on this line and definitely not anything down here so the weight because we always have positive numbers how do we can get a negative number now multiplying it by a thousand will guarantee since this at most will be one that it will be within a thousand units of the center of our scene which is good because if they if they if they were too far off you wouldn't see them if they're more than ten thousand off they'd be out of your clipping view if the cameras near the center of our scene but how do we get a negative number sometimes and not others well that's one of the reasons we have a such a small number for our random here so what we're going to do is we're going to put in parentheses because we want this to happen before we multiply by a thousand we are going to subtract point zero five from whatever the random number that is generated so maybe every from zero to one but then we're subtracting negative five and that will get us some negative numbers in there that we're then multiplying by a thousand will refresh here and now you can see we have random if this is a center of our scene we have some cubes in all areas now let's up the number of cubes we're generating so we've created ten let's create a hundred cubes at five there we go now we're really getting the numbers here going here so I can zoom out again at most if the highest number that the random number can give us is one and then we're subtracting five from it it's going to be anywhere it will be the highest number we can get is point five and if the lowest number we get is zero then the lowest number after subtracting point five is point negative point five so we have this nice range from negative point five to negative five giving us a balance of positive and negative numbers so it's going to be a half of a unit positive half of the unit negative times a thousand so really at most these boxes are 500 units away from the center of our scene so what we can do is let's double that spread our boxes out a little bit more because at most from the center there are five boxes apart which means we're gonna get a lot of touching all depends on how grouped you want your cubes so I'm gonna hit at five here to refresh and there we go there a little more spread out now and of course again you can increase that number that you want now all the cubes currently and again I'm rotating the camera here not the cubes using the trackball library from 3js they're all still lined up none of them are randomly rotated so let's go ahead and adjust the rotation of these objects so I'm just going to save time I'm going to paste in copy and paste some lines here wrong line I can hit the right keys here undo paste there we go and change all these say position to say rotation now you might think oh great now we're gonna get random numbers for rotation as is spell things right thing is this would probably work let's see it does work but in reality we are also rotating a lot more than we have to so although that works you could also put in much lower numbers than what we currently have in here so I'm trying to think of what the rotation numbers are so even just that or even probably just doing random and by rotation numbers they are I'm trying to think of is a complete the rotation in this what what numeric value so just putting random in there will give you random rotation if you want to again rotate a certain number of degrees we did that in a previous tutorial it's take pi divide it by 180 and then multiply that by the number of degrees but since we're random here it doesn't matter so that seems work just putting random in there I have seen other people in example scripts use other mathematical equations I'm not sure doing random maybe it's not really giving a real not there are real random numbers in computing but maybe it's not giving the full rotation maybe none of these will have a full rotation ever because the random number never reaches its maximum but for this tutorial just saying that to random seems to work well and of course so I hit f5 to refresh there if I f5 again you can see each time I hit f5 to refresh my page I'm getting randomly theoretically randomly generated cubes and random positions with random rotations so that's it for this tutorial I hope that you found it useful I hope you're finding all these trailers useful again a new video every Friday if you're watching the playlist and you hit a video that's private it's because it hasn't been published yet a new one will be published next Friday if you're enjoying these 3d HTML tutorials using 3js be sure to like this video so I know also comment below let me know what you think if you have questions rather not put them in the comments come and ask me in the IRC channel visit my website filmsbychrist.com that's Chris of the K should be a link in a description click on the tab that says social networking and click on IRC and that will bring you to my IRC channel where I hang out regularly but it's not just me in there there's other people in there just come talk hang out and I hope that you have a great day