Random Number Between 1 and 100
Learn how to generate a random number between 1 and 100 in Google Sheets using formulas and Google Apps Script. This video also covers creating a custom menu for easy access to the random number generation function.
So you wanna generate a number between one and 100, and you want it to be random. We can do ran between as a formula , we can say the low is one, the high is 100, and they're separated by a comma. So comma, if we are in a locale with a comma, if you're in somewhere else, not the us, sometimes it's a semicolon, but that'll be across the board for every formula . But let's generate this random number and we enter once and this formula will do it. However, anytime you edit the sheet, the number changes. So we want another number there. We have two different numbers, but these numbers will change. So let's quickly make a little automation up here that will
do this once and only once. Go up to extensions app script and we have our formula . So we're gonna copy this and have this ready to paste. We're gonna write a function , generate number one through 100. We're taking the exact spreadsheet , the entire spreadsheet , file, spreadsheet , dot. App, spreadsheet app, adding parentheses, then dot then get active. Just the dot, not the parentheses. Get active range. And we're gonna set a value. And that value in quotes is that formula that we copied before. So this is. Equals ran between one to 100. Then we're going to take the spreadsheet and flush it. This means that we will execute that.
Then we'll go back to that range . Let's copy and paste it so we don't have to type it again. We'll copy it to. Itself. And we have some options that we can do. And one of them is curly brackets, contents only true. So I'm actually going to paste that. And that's curly brackets, contents only with a capital O. True. Let's save this. And C, what happens if we don't even have anything here? This is blank. And we're gonna have this active range here. We're going to hit run. We will have to do permissions, authorize this the first time we use it. Okay? Continue. We don't have any errors and we have a number . Let's run that again, and we have a different number . Cool. But we don't want to have to come to this app script here and have to do this each time and hit run open app script , hit run so we can allow this to be.
Available to any user of our sheet. If we use a custom menu , I can go to better sheets.co/snippets. I don't have to be logged in for this. This is free for everyone. Better sheets.co/snippets. There's a bunch of app scripts we can copy and paste into our sheets that are just gonna just make our sheets way better. And we have this one thing called a custom menu . This is Function on Open. I'm just gonna copy and paste this up here. It's Function on Open, which is a automation that exists in app Script without us having to create one. We just literally type this function on open. We're getting the UI of the entire sheet, and we're saying, Hey, add a custom menu here and we're gonna call this Generate Rand, um, number . 1 2, 100. And then we're going to take the function name, generate number one through
100, and put it as the second element. So the first element here in this ad item is what the user will see. The second one is what the computer will do when clicked. So I'm gonna save this and I'm gonna go to the button next to running and click this and save. And then I'm gonna close my app script . I can. Also refresh my sheet and it'll, after saving, it'll close that app script . I'm gonna select B two. Actually, let me see. Now we have Gemini extensions help, accessibility and custom menu , click Custom menu . And there is our function and the name that we wrote that says, Hey user, this is what you're gonna do. I'm gonna click it. And then it ran it. I don't know if you saw it, it went very quick, but it actually did put
the formula there and then it copied and pasted it with only the value. Wasn't this cool? So this is a really interesting way to use App Script to essentially do what we were doing before with a formula , but now it's set here. Now I can do that again. Generate random number one through 100 and it's gonna see, you can see it changed the number in that range . Very, very cool, right? Hope you enjoy this little app script and if you wanna know more, take a master spreadsheet . Automation 1 0 1. It is a course I offer app beta sheets co, but also standalone. Uh, if you wanna just get it at Udemy for like nine, 10 bucks. But if you want monthly membership gets spreadsheet automation 1 0 1 within it and a whole lot of other stuff. over@bettersheets.co. You are watching better sheets here on YouTube.
Make sure you check out this video or this video and subscribe right now to get more tips, tricks, how tos, get more out of your Google sheets than you ever have before. I'm excited to be making a ton more videos here. Ask me questions down in the comments and I will answer them in future videos. But for right now, right here, one of these videos is gonna be your next Google sheet.