How To Generate Random Number Once in Google Sheets

Learn how to generate a random number once in Google Sheets using a simple app script, ensuring that the number remains unchanged even after editing the sheet.

Let's say I wanna generate a random number . I'm just creating a dummy list here, just a table of information, just to look like information. And I can do Rand, I can even do Rand between as a formula , and I can say Rand between let's say 50 and a hundred. But the issue is every single time that I edit this sheet, like let's say I move this to March, see everything changes every single time. I. Just change one thing. All of these change, so I wanna generate a number once and only once, and then have that random number stay the same forever. So how do I do that? Well, I'm gonna go up to Extensions app script . I'm gonna do a little bit of coding. Super easy. Just follow along. Here I'm gonna create a function called generate. Random number and I want to have a minimum and a maximum.

So variable min equals 50 max, variable max equals 100. That's just like saying, Hey, between these two numbers, get a random number , and now I'm gonna get that random number . Random number equals math with a capital M, and make sure it's a capital M, and then it turns to pink or purple floor. And we're just saying, math Flores is saying of these two numbers, get the lowest one math random with parentheses times. We wanna do max minus min plus one, and we want to add one at the end after these two parentheses. Sorry, add the minimum back in after all of this random is done. And now here's the interesting trick.

We can do spreadsheet app dot, get active range . Do set value, random number . Now how are we gonna. Use this over here in the sheet. Well, we're going to add another function up here, function on open, and here we can go to better sheets.co/snippets. And here in Better sheets.co/snippets. We have a custom menu . We have this unopened menu we can copy and paste. We don't need to memorize this. We just need one item. And we don't need document or app form app. We just need spreadsheet , app dot ui, create a custom menu , random generator, and this first item is going to be generate random number,

but we're gonna need for the second item, the exact function name. There we go. Now that is all we need. We can save this. Now refresh the sheet. Once refreshed. Next to that help menu at the top is random generator. I'm gonna select B two. I'm gonna select random generator and click generate random number . It will probably ask me for authorization the very first time that I run it and there is a random number . Let's go to B three Random. Generate random number 57, and this is not a formula, it is just text or rather a number, number string here. So it's going to stay the same no matter if I edit these cells can just generate another number, generate another number, generate another number as many times as I want. And again, the past ones are staying the same.

It's not. Changing every single time. Isn't that really cool? You can generate a number once and leave it as that number . Hope you enjoyed this and get this exact sheet down below in the description. 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.