Google Sheets – Random Alphabetic, Random Alphanumeric and Random Alphanumeric + Character Custom Functions

randalpha examples = Google Sheets

Google Sheets, Google Apps Script

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?




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)



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


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.

Random Alphabetic Google Sheet

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

Random Alphanumeric Google Sheets

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

Random Alphanumeric and Characters - Google Sheets

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:

RANDALPHA UPPER LOWER - Google SheetsError 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.

RANDALPHA Error Handling - Google Sheets


  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.

Adding RANDALPHA to your Spreadsheet

  1. Open up the Google spreadsheet you wish to add RANDALPHA function to.
  2. In the Menu Bar go to Tools > Script editor
  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, go to File > Save 
  8. Close the Script Editor
  9. In your spreadsheet test the RANDALPHA  custom function.

RANDALPHA installation

The Code

Looking to learn more about Google Apps Scripts in a more structured format? Udemy has some great courses that can get you from the basics to a real Google Apps Script pro!

Got a more specific problem you need help with, but don’t have the time to develop the skills? Fiverr’s your best bet to find a skilled Google Apps Script professional to solve your problem quickly and cheaply. *

*The above affiliate links have been carefully researched to get you to what you specifically need. If you decide to click on one of these links it will cost you just the same as going to the site. If you decide to sign up, I just get a little pocket money to help pay for the costs of running this website.

17 thoughts on “Google Sheets – Random Alphabetic, Random Alphanumeric and Random Alphanumeric + Character Custom Functions”

  1. 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. 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 ?

  2. 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?


  3. 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?

  4. 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. 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){

Leave a Reply