 And today, we're going to be looking at creating random numbers in your bash shell scripts. And yes, I know, most of you know this. Let me just say off the bat, computers cannot generate true random numbers. They just can fake it very well. And the techniques we're looking at today are very basic if you just need to like choose some random numbers. But they're not the best ways to random numbers. But for most of your random funness, if you're not doing something like encryption, this should be good enough. So here we are. We're at a bash shell. I can say echo. And there's a variable called random with a capital R. And if you echo that out, each time you echo it out, it should give you a random number. If I needed a list of random numbers, I could do something like dash e. And then I can say backslash n. And I can do like this. And I can just say, give me a couple of random numbers. So the backslash n, the e is saying, look at any special characters like this backslash n and use it as it, especially as it is. That didn't make any sense. The e is saying using the backslash n as a new line. So I can do that. And now I got a list of random numbers. And each time I run that, I should get a new list of random numbers. Another way I could do it is I could put it in a for loop. I could say for i in, then I can use brace expansion to generate, well, here I'm counting from one, we'll just say one to ten. And we'll say do echo dollar sign random. And what this will do is it will loop ten times, and each time it loops, it's going to echo out a random number. Great. If you want to get a range, so you want to, let's say, you want to get between zero and 99, or one and 99, what you can do is you can say echo, and in bash you can use double braces to do a little bit of math, so we can take that random number, so we're using the same variable as before, and we're going to say percent sign, so we're going to get the remainder here, and we're going to say 100. And that should give us a random number between zero and 99. If we want between one and 100, we will add one to that. So that should give us between one and 100. So that's one way to do that. Another way to get a random number between two sets is we can do echo, and I find this is a little bit easier and clearer for me anyway to read. We're going to say, again, that random variable, then we're going to say colon, and we're going to say zero colon three, well, we'll say two to start off. So what we're going to do here, we do that, and what it's doing is it's going to give us a two-digit random number, so this is from zero to 99. If we say three, we should get somewhere between, we should get a three-digit number. I forget if this, yeah, so it's given 24, so this is from zero to 999, so it's going to give you a max of a three-digit number. So that's another way to create randomness. Now, before I show you, well, now I'll show you one other way to do this, is the shuff command, which may or may not be installed on just my default, but you should be able to install it. It is part of Busybox, so anywhere you can get Busybox running, I'm pretty sure Shuff is part of it, so pretty much any Linux system, your phone priority has it, and as I've shown in previous videos, you can even get Busybox running on Windows and it has Shuff. Shuffles things, and you can give it files, and it will shuffle the files, but you can also say dash i, I'm assuming for integer, and give it a range of numbers. So if I want 1,000 to 5,000, so I can get a random number between 1,000, 5,000, if I do that, it's going to give me a whole list of number. I'm not sure how many, let's just do a word count on that, I'm not even sure. So it looks like it stops at 4,001, but what you can do is dash n and give it a number, so I can say I just want one. I just want one random number between those two. And to me this is a little bit longer and it's not built into the Bash Shell, it's an external command, but it's a little bit easier from my mind to remember. And if I want 10, I can say 10, give me 10 random numbers. Okay, now, we've been working in the Bash Shell. Let me real quick show you something. I'm now in a Z Shell, which is a different shell and there are some differences. I can still echo random and it will give me a random number. I can also say echo random and give it the new line character and give it a few of those. Note that the Z Shell, you don't have to say dash e, it automatically looks at the backslash n as a new line character. And there we go, I can get myself some random numbers. I can also do the for loop. I can say for i in and I can do brace expansion like this. I'll just say 10, do echo dollar sign random, done. And we should get 10 random numbers each time. So far so good. I believe we can also do the brace, curly braces here. Say random, I'll say zero, colon two. So we should get anywhere from zero to 99. Yep, yep, so there we go, we have that. Then we can put that in the for loop as well. But here is a difference between bash and Z Shell. Let me go back to the bash shell for a second. So let me go up and I'll say, here we go. So I'm creating a loop, I'm creating 10 random numbers. I'm gonna put this into sort. Now, I'm gonna do all of the video on sorting numbers. This is not the correct way to sort numbers, but watch this. So I do this and it's sorting theoretically in numeric value. It's actually going from, you can see the first column is one, two, six, eight, nine. So it's kind of sorting it, but not properly numerically. But it is sorting them somewhat each time giving me random numbers. Let me go ahead and take this same command, go to my Z shell and run it. So same command that before without sort, let me show you. It does give us random numbers. They are different numbers each time. But for some reason, if I pipe that into sort and I keep doing that, it's giving me the same 10 numbers each time and I have no clue why. And that's true also if I come up here, for example, this one. I run this a few times, you can see it's giving me different numbers each time. I pipe that into sort, all of a sudden, every time I run this, it's giving me the same numbers. And I have, again, no clue why, but shuff does work. If I go up to the shuff command, did we not do that in the bash shell? So I'm going to say shuff dash I. I'll say from 1 to 1000 and I'll say give me 10 of those. So that works, that's an external command. It's not built into the shell, but either is sort. But if I sort that now, it will continue to give me random numbers. I do not know why the other sets give me the same numbers over and over again when I put them into sort. Again, when you're sorting numbers, this is not the appropriate way to do it. As you can see, it's not sorting them properly. It's sorting them based on, like this starts with one. So it's thinking that it's lower than 21, where obviously 119 is higher than 21. But we're gonna get into that in another video. I just want you to be aware of that though. So I showed you a few different ways to generate random numbers and list of random numbers. But for some reason in the Z shell, most of those ways, if you pipe it into sort, it stops giving you random numbers. It will give you one set of random numbers and continue to give you those over and over again. Anyone know why? Comment below. But I came across that and I just wanted to bring it to your attention. So thank you for watching. Please visit filmsbychrist.com. That's Chris Decay. There's a link in the description to my website where you can search through all my videos from both my channels. And you can also check out my Patreon, patreon.com forward slash mylex1000 if you'd like to support. There's a support section on my website and I hope that you have a great day.