 Now one thing you occasionally need to do on the command line or while you're writing scripts or programs or something like that is The ability to either generate random numbers or you know pick a line randomly or just do stuff That is random on the command line. So in this video I want to talk about how to do some things randomly in Linux or bash or really the command line generally there are a couple different ways and Some of them are easier than others. Some of them are more extensible But I want to talk about them regardless because a lot of people will care I've seen people rely on things. They don't need to to do stuff like this. So At a very basic level this we'll start with the less useful more. I guess abstract random aspects Now you may know that a good Unix system a good Linux system has this thing called dev you random And if you cat it out, you're actually just gonna get a whole bunch of random characters Now dev you random you can look it up I'm not gonna go into the specifics of it But let's just say that dev you random is used to generate Cryptographic keys its entropy is very I guess Taken care of so to speak, you know, it's it's the most random You can pretty much get on a Linux system and it's used by a lot of the the other random tools We'll talk about so there's dev you random and also dev random Now anyway, let's talk about more practical tools I mean you're not just gonna want to generate a whole bunch of characters and do something with that You probably want something more sensible like numbers or well anyway So you may know I assume that most people are using bash And if you are there is a special variable called random now if you echo random You are gonna get a number a random number not just any random number. You can actually check the manual for bash let's do that for a second and You can look it up and you'll see that what this variable does is it generates a number between zero and 32,767 now that you know practically speaking that sounds a little arbitrary When do I ever need a range like that? Well, how people usually use random is you know one smart little hack that people do for it is let's say we want to a number between You know one or you know, we want a range of 10 basically what we can do is we can actually do bash arithmetic and Get the modulo of random by 10 and what that will do is effectively give you so modulo I guess most people might know but modulo is if you divide this random number by 10 What is the remainder? So what this means in this respect is that we're gonna get a number I think from zero to nine. Yeah, and so effectively this is 10 Or if you actually want it to be able to get to 10 you can actually say and let's let's add one to that So I think we can get a 10. Yeah, so there's a 10. So that's a good way of random number generation as well It's a little strange, of course because you do need to do Some bad bash arithmetic and you can't manually specify exactly what you're doing You're sort of doing it or beating around the bush so to speak But you can do it with pretty much any kind of number You can say you know a random number between one and a hundred and run that over and over again So that's one possibility and that's usually the quicker possibility But the drawbacks are one it looks a little ugly and two it also is bash specific So if you're using another if you're using using some other kind of a shell let's say dash Which is very ugly because my ps1 is sort of funky looking But you know, let's say you're using something like dash if you echo random Nothing's gonna happen. This is a bash specific thing. So on Linux. There is another tool that's pretty nice called shuff Well, it's actually a command you can look it up yourself and I guess it's short for shuffle I assume that that's what that's for now, of course, this is this is Linux specific I don't think that it comes on BSD or Mac unless you specifically install the core utils But I think BSD does have an equivalent called jot, which isn't exactly the same But it sort of serves the same purpose. So what shuff does So we'll we'll show you what shuff does by default and you can use it for random number generation or other stuff but So I have a file on my computer that I it's basically a database of emoji Okay, so if you watch my channel, you may know I have this nice little thing if I want to type a cake emoji I just have this nice little window here that I can select An emoji from and it basically reads this file that lists out all the emoji with their Names, okay, so I have this little file. Let's say I want to well actually Let's just run shuff on this file and see what happens What happens is it actually goes through the file takes every line and it puts all those lines in a Random order. So all these, you know, this is not the natural order of this file It in fact if you cat it out, you'll see it actually ends with flags and stuff like that But if I run shuff on it It will put those lines in a random order So let's talk about some other things you can do here. So I can give it the in option Let's say I only really want 10, okay I can get 10 of those and it's just gonna give me 10 and that's it or let's say we only want one I can have one One and only one emoji. Now you might ask yourself, okay So shuff right now as it is is actually just Randomly shuffling up the the lines in the file. It's not duplicating them, but let's say Actually, I'll give the example of numbers first in addition to giving it files You can also give it the I option and let's say we want a number between 1 and 10 And it's going to basically take that range from 1 and 10 and it's going to Randomize that okay, so these are all random and if we want only one of those again, we can give it the in option And we get eight so one question you might have is that or you might notice of course Is that it's only using each one of these numbers once it's not like randomly drawing one through ten Ten times because each of these numbers is only used once So when we get down to here the chance of the last number being eight is a hundred percent because every other number besides eight has been used But if we So let's say we run it with the our option it actually can't it doesn't exhaust any of the options It just continually prints them out. So if I run if I say I want ten Draws from one through ten, but I want you to repeat the numbers because I gave it the our option You'll see that it generates these numbers, but there are actually some duplicates. So two appears twice Five appears twice there might be another one that I don't immediately see but so if you give it the our option It can repeatedly draws from a file. So this can be important I mean it all depends on what you're trying to do if you're just trying to shuffle something up if you're just trying to You know, let's say give give options to someone. Let's say you're doing in an experiment or something like that Let's say you just want to give options to someone in a random order You don't want to use the our option because you only want everything once But if you just want to generate some random numbers or might be a good option Um, so let's play around with this just because why not so as an implementation Let's say so I mentioned a second ago. So I have this emoji file So if I want a little command that randomly generates an emoji I can of course give it, uh, you know one and okay, I got the Ecuador flag got a drum got a zombie Now let's actually go a little further Let's cut out because I only really want the emojis So I'm going to cut out Um Everything but the first element here. So we got this little arrows a bcd Is that a noose or something? I What is going on? Anyway, so I can get random emojis like this Um, and I'll play around with it. Let's say so I'm going to add to my status bar up here Just because why not just because we're doing a video on it Um, I am going to add to my status bar this little command I three blocks Totally forgetting where everything is I'm going to add an emoji um Module and I will have it run this command shuff config emoji and give me one and cut out The first thing before the space and I'll say interval equals once Okay, so if I run that and I refresh you will see that I now have this little check emoji here If I click on it because I three blocks will update every time you you click on it. Now we have a little Pink pantsuit or no. No, that's kimono or something. So I can click through those actually. I should probably make it bigger because why not Why not because we're doing a video Okay, so we can so we can click through these or I guess since I'm using I three blocks I can just say interval one second And then it's going to update every second So now now we have just random emojis now Anyway, this particular implementation probably totally useless and unless you just want to have some fun with it But I know a lot of you guys use Something like cal say or one of these decorative Things to generate stuff and shuffle is nice for using for that But of course as I said, you can also use it to generate random numbers to generate random ranges to shuffle them up And really to get pretty much whatever you want. So this is uh, I guess that's about it for this video But if you have any questions check the manual, but you can also ask in the comments if you want And I will see you guys next time