 Hello, and welcome. This video is part of a series, number six in the series. Last video, we looked at the basics of arrays, creating arrays and looping through them, iterating through them. Today, we're going to look at shuffling arrays, which is very useful in many cases. You might have a large list of stuff and you want to grab one randomly or you just want them in a random order. We're going to do that, and we could do it all in our main function, but we're going to start working with other functions now because it's good for something like this where you want to shuffle an array to have a function that does that. So if you have more than one array, you can just pass it to that same function rather than repeating code, and that's what functions are primarily for. So we're going to, again, I'm using Vim as my text editor. You can use whatever you want, and I'm going to try trying to keep these video shirts. We're going to go through this fairly fast, but we're going to have to include a few other headers here. So include, and here we're going to say stdlib.h, and we're also going to include time.header. Now, we can create a function. We could create the function above here, above main, if we want to. We can put the whole function there because a function has to be called, has to be written before it's called. So we could write the whole function up there and then call in main. If we wrote it down here after main, main wouldn't know of it. So what you normally do, you want your main function at the top. So what you can do is you can declare the function up top and then actually write it below. So what we're going to do here is I'm going to say void shuffle is what we're going to call our function, and it's going to take some input. So we're going to say char, astrict, array, or r for our array, and then an integer, which is the length. So we're going to pass it the array and the length of the array. Here's our array here. Let's go ahead and take that out and make it a global variable for now just to keep things simple. And then down in here, actually I don't know if I have to put that outside the main function. We'll leave it outside for right now. But what we're going to do is we need to generate a random seed. So computers can't really generate random things. They're all based on math. But that's why we have our time function. It's going to use the time function, some time stuff to fake a randomness. But we have to create a random seed. So we're going to say s rand, so a function, and we're going to pass it time another function. And then here we'll put zero. So I call that is going to be our seed that we're generating. Then we've got our array. And then we're going to call our array. I'm sorry, our function, which we called shuffle. And what are we going to pass it? We're going to pass it my strings and the length. So we're passing it the array and the length. And at this point, I'm going to close off this function and return zero. Okay, great. So we've declared our function. Now let's actually create the function. Again, we're going to say void here. And we're going to call it shuffle. And really, we can just copy this from up here would be a little quicker. Okay, just doing a little copy and pasting there to save time. We don't need this return because this is a void function. We could return the array after shuffling it. But we're just going to take that array, and we're going to print out our message here and then loop through it, but shuffle it up. So let's go ahead. We clean this up a little bit from our previous video. So we're going to print out the number of items in the array, which is the in here, we're going to recall this Len, because that's what we're passing it as. We called it array length here. Actually, there we go. That saves us a little bit of trouble. And down here, we're going to say a Len again. It all depends on what you want to name your variables. Oops. Okay, things are going to change a little bit here. We're going to print out the count. How many things are in there? They're going to put this little line break there. They're going to start our for loop here. We're going to say integer start at zero. As long as it's shorter than the length of the array, we're going to add one to it. Now, before we print out our message, we're going to create another int another integer variable. We'll call it pick index. We're going to say equal ran. So we're using a function, a math function to create something random. And we're going to say percent, which is going to find the remainder when doing division. And then we're going to take the length that is passed to it, and then subtract I, which is what we're iterating through the variable here, which would be zero, then one, then two, then three, and so forth and so on. Now, when we print out our message here, and we don't need to say item whatever because we're shuffling it. So it isn't really that anymore. And here we renamed our array to ARR. We're going to now say to use our pick index instead of I. So basically we're getting the length minus one of the array. And we're basically taking that number and saying find a random number that's no bigger than that. So basically zero between that number. And we're adding that into here. And then we're printing out. Now, if we did that now, it will kind of sort of work. If I typed everything right, let's go ahead and pile that and run that. You can see it kind of sort of worked. It gave us a random one each time. So if you want it to each time, give one of those, in this case, seven different things, it will do that. But we want to actually have a shuffled list where each item shows up once. So it randomly picked four, then it randomly picked six, then it randomly picked four again, and then four times in a row, it randomly picked one. You may want that. You may not, depends on your project. But what we can do next is remove that item that we just used from the array. So we're going to say array pick index. So that's the one we just printed. And we're going to say, from the array, take the length minus the current integer minus one. And what that's going to do is each time an item in the array is used, it's going to be removed from the array. So now we should be able to recompile that, run it again. And now, instead of getting a random one each time from the entire list, we're getting a random one each time from what's left in the list. So we shouldn't get the same item twice. So as you can see, six, one, two, four, five, three, seven, run it again. Now we got six, five, seven, one, four, two, three, run it again. One, two, four, seven, six, five, three. So what we've looked at here again, we're looking at using this standard live and standard time to create some random seeds and some random numbers that are within the range of the length of our array. And then we're going to print out a random item from the list from the array. But then we're going to say, look at that array and remove that item from the array. That's what we're doing here is we're removing that last used item. It's gone so that it cannot be used again. So again, if you wanted to show a random item, and you don't mind if it's picking the same number each time, you leave this line off and comment it out like that. But if you want to have just shuffle the list, we're shuffling the list. This is what we're doing here. You're making sure that you don't use the same item twice. You're removing it from the list. So I hope you found that useful. I thank you for watching. Please visit filmsbychrist.com. That's Chris Decay. There's a link in the description there. You can search through all my videos, which there's plenty of. You can look at the software section, which has a link to my GitLab page, my scripts page and my notes page. Scripts are basically some scripts I have online. Most of those will be HTML, JavaScript stuff that runs in your browser. Notes is a place where you can search through basically everything I have up on Payspin. Currently there's 702 entries. And those are like quick little scripts. And then if you want bigger projects, I have my GitLab page. And if you actually go there and go to gitlab.com.com. And then do forward slash my bin, capital M, capital B. These are just like a package of basic little scripts of different languages, just the basics of them. And if you go to see tutorials, you'll find the examples or at least similar examples that go along with this series of videos. So check that out. I do thank you for watching. Again, visit those channels. If you do like my videos, think about going to patreon.com for slash malix 1000. You can click on this link in the support section of my website. It's become a monthly supporter if you want to do a one time support. PayPal right here is a little link you can use. And I do appreciate any support. If you can't do those things, think about sharing, liking, subscribing, all those things commenting below. And as always, I hope that you have a great day.