# 5 Ways to Generate Random Numbers

generate random numbers using Google Sheets. Fun ways to figure out how to get random characters. Great to use for creating IDs or Coupon Codes. Nice easy intro to some simple apps script.

Sheet Resources

Featured Formulas

Video Transcript

Hi. In this video I'm going to show you five different ways to generate random numbers. We're going to do it off of Google Sheets. We're gonna do two within formulas, and then we're gonna do two that are scripts. So get ready. This is gonna be fun. So if you wanna generate a number, we usually can do something like we can generate random numbers in Google.

if you can spell random number, Google has this great random number generator. You can generate a number between any numbers, like 101, whatever that number is, generate, and it'll give you a number. Great. Now this is pretty simple. If we have wifi, we can just copy paste that number right in and create a gener and generate a completely random number.

But sometimes we want to do this within Google. And we can do that by just hitting the equal sign rand and having these parentheses. And we get a pretty random number here. It's actually a number that is between zero and one. What's interesting about this is that then you can take this and multiply it by, let's say a hundred.

Now we're gonna get a weird number, like 13 point blah, blah, blah. But we can round it, we can say,  and now we're gonna get a number between one and 100 every time because we always get a number between zero and one. We multiply it by a hundred. We're always gonna get a number between zero and 100 now. But if you're really, if your real need here is absolutely to pick a number between some number and another number we can use ran between, we can just go into the cell equal ran between, and what we're doing is we're gonna say between one.

20, let's say we're kind of like rolling a 20 sided dye. We can copy paste this and we can get a new number every time. But if you notice this, rand is also changing. What's going on is that these two are changing. Every time there is an edit, so even if I go back to our random number generator here, generate a number, copy and paste this.

The other numbers are changing. The other numbers are changing every single time I edit the sheet. All right. What hap why? Why we might not want that , we want a random number and we just want it to stick. So what we've usually done, I've usually done is I'll generate a random number. Sometimes it's a pretty big number, something like this.

And then I will command C and I will shift command V two, copy and paste that number. And now see, I ha I'm generating a new number every time I'm copying and pacing it, but I'm pacing the values as I go across the sheet. So that's pretty. Symbol, but again, not that best. So one other way we can do random number generating is we can go up to Extensions app script

and once this loads, we'll say function. My function. We can call this generate new number. Number. And here we're gonna do math dot random. We're gonna hit command S to save, and now all we need to do within a cell is have this generate new number. So we go equals generate new number. And with the parenthesis it will show up red, but it's okay.

It's gonna load the data. Loading, loading, loading, loading, taking a while to generate our random. . Ah, I see what happened. I didn't return anything. So we gotta do return. So with the function, you need to add return here, and now we get a number. And as we edit this, so again, we can take this number, copy paste, see the number that's generated is not changing.

So this script, while it may take a little bit of time to get it to come out, we're.  we're also still in a decimal number, so we can take this number and multiply it by 100 command s go back to our number and should change 38. But now we need to round it so we can do math floor

C command S to.  and now we have a perfectly good number, a totally random number that's not going to change as we copy and paste things around. Perfect. , but whenever we wanna generate a random number, we're never really just generating a random number. We're doing it for some reason. The reason might be to roll the dice.

We might be creating a game within a Google sheet. We might be using Google sheets to run a game of d and d of, of sorts or something. Or maybe it is mere. Randomizer For other games like poker players might use Randomizer for their game play. So how do we do this? How do we create like a custom function?

This one I really did rename generate a new number, and this is actually a custom function, but we can create another one. We can say function, role, the dice.  and we can get two. We, we might want to get some input, like maybe we want a function that we say how many sides of the dice we have. We might wanna say 10, or we might wanna say 20.

So we can have some number here. So we can do a numb. Now we have to have these curly brackets to actually run the function. And what we're gonna do is we're gonna return.  the roll. How do we get the roll? We do variable sides equals number. This is actually needless cause We can actually write this as sides, but we'll just wanna show you how this sort of works and what we can do then.

We need to do the roll variable. The roll equals math, floor, math, random times, sides. So now every time we roll the dice, we're gonna get, get a number. That number is gonna be the number of sides, which is just multiplying this random number and getting this floor, and we get the return, which is the roll.

So let's try this out. Roll the, And we're gonna go equals roll the dice with 2120 sided dice.

Get an error. Oh, I misspelled random, not Randy. Om random.

There we go. So now we rolled a 20 sided dice. Maybe we roll a 40 sided. , maybe we roll a 10 sided dice

and now we can even roll a two-sided dice. And that was five ways to generate random numbers. Hope you enjoyed this video.

Better Sheets