# Google Sheets – Random Alphabetic, Random Alphanumeric and Random Alphanumeric + Character Custom Functions (Updated January 2022)

I was working on a Spreadsheet in Google Sheets a few days ago and needed to generate some random codes for my users. To do this, I just used the `RANDBETWEEN(start val, end val)` function built into Google Sheets.

It goes a little like this. If I want to build a 5 digit random number I would:

But this got me wondering. Is there a function for random Alphabetic strings or Alphanumeric strings or Alphanumeric with Character strings. The answer?

via GIPHY

No…

So instead I decided to make a custom function with Google Apps Script to do this job for me…

### The RANDALPHA function.

The RANDALPHA function is a custom function that you can call up just like a normal function in your Google Sheets (once you have added it to your sheet).

As the name suggests, RANDALPHA can generate 3 different types of random codes at a character length of your choice. To create a random code you simply input the length and choose from one of three different randomisation options.

Input is as follows:

`=RANDALPHA(character length, type of random number)`

or:

`=RANDALPHA(10, 0)`

Resulting in a 10 character long random string of Alphabetic Only text:

`QtqjsyWsjj`

Type of Random Number

There are three types of random numbers that you can add to the second argument of the RANDALPHA function (0,1 or 2):

1. Alphabetic: the function will generate a random string of alphabetic characters with both upper and lower case letters.

1. Alphanumeric: the function will generate a random string of alphabetic and numeric characters.

1. Alphanumeric + Characters: the function will generate a random string of alphabetic, numeric values along with characters.

Purely Uppercase or Lowercase Characters

If you just want to make your random code uppercase or lowercase you can simply add the Google Sheet Functions UPPER or LOWER and wrap RANDALPHA inside it like this:

Error Handling

If you make an error by adding a non-numerical value to one of the two arguments or attempt to choose a type of random number greater than or equal to 3 then you will get the following errors.

?You can support me for free by using this Amazon affiliate link in your next tech purchase :Computers & Stuff! ?

Warning

1. There are no outer limits. You could make your character length any number of characters in length, but if you create enormous codes like something crazy like 10^100, it’s probably going to break or put your spreadsheet in limbo.
2. Custom functions run from the server-side, not the user-side, therefore, they will be slower to generate the function values.

1. Open up the Google spreadsheet you wish to add RANDALPHA function to.
2. In the Menu Bar go to Extensions > App Script
3. In the top right-hand corner, there will be a title for the Project. Rename it to RandAlpha or the title or your spreadsheet or whatever you want…
4. In the Code section, delete out all existing code.
5. From the code below in this post copy all the code.
6. Paste the code in the Script Editor
7. In the Script Editor, click on Save Project
8. Close the Script Editor
9. In your spreadsheet test the RANDALPHA  custom function.

The Code

Need help with Google Workspace development?

My team of experts can help you with all of your needs, from custom app development to integrations and security. We have a proven track record of success in helping businesses of all sizes get the most out of Google Workspace.

Schedule a free consultation today to discuss your needs and get started or learn more about our services here.

Posted on

## 19 thoughts on “Google Sheets – Random Alphabetic, Random Alphanumeric and Random Alphanumeric + Character Custom Functions (Updated January 2022)”

1. Stewart says:

This was super helpful and really easy to understand! Thank you!

1. Yagi says:

Glad you found it useful

2. Melany says:

I need to generate 2500 unique 3 digit alphanumeric codes. Only using uppercase A-Z and numbers 1-6. Is there a code to do this in google sheets?

1. John says:

Hi Yagi, Great script and really clear instructions – many thanks for providing the source. I might be missing something but couldn’t Melanie just edit the charString and charStringRange in your example above to achieve what was requested ?

3. piewww says:

Great script ! Thanks !

1. Yagi says:

Thanks for the kind words.

4. Juan says:

Hello! first of all, thanks for sharing! I see the post is a little old, but I decided to give it a try and write.

I’m always getting NaN as a result when trying to use your script, any ideas in how did I do wrong?

Regards!

1. Yagi says:

Hi Juan,

It should still work fine. What is your input into the custom function?

~Yagi

5. KCMike says:

Try this….

=DEC2HEX(RANDBETWEEN(0, 4294967295), 8)

1. Yagi says:

Great to hear, Lucas. Thanks!

6. Pete says:

I know this is a long shot but here goes. I have a sheet that users input requests via a google form, and I want to assign a unique ID to each row. Is there any way to apply this function to a column using the arrayformula function so that each cell gets a unique value without havin to apply the function to each row individually?

7. Jack says:

Thank you very much for sharing! this works for me.
However, I have an user onboarding Sheet having the randomized password in it, the problem is, the password generated by function will change itself after some time, how can we stop that?
this Sheet is also used by powershell for onboarding automation, if it changes, the password is not right for future reference.

I didn’t find a way to stop it from changing yet, hope you can shed some lights

1. Jack says:

WOW, thank you very much for the hints Yagi! onEdit is a good way to do it.

I just added your function to onEdit and it works great, won’t change anymore!

function onEdit(e) {

var row = e.range.getRow();
var col = e.range.getColumn();
var as = e.source.getActiveSheet();
if(as.getName() == ‘OnBoardingSheet’ && col==5){
as.getRange(row,5).setValue(RANDALPHA(15,2))
}
}

8. Esther says:

Thanks for sharing! It is very helpful. I have just used this on one of my projects.

1. Yagi says:

That’s wonderful to hear. Thanks, Esther.