Generate a Random Number

There are a number of reasons why you may want to generate a random number within a survey. For example, you might want to assign a random number to each respondent that can serve as an ID number.

There are a couple of ways to generate a random number in your surveys. Most commonly, this is done by adding a Hidden Value Action.

Generate a Random Number with a Hidden Value Action

Follow these steps to generate a random number with a Hidden Value Action:

  1. On the survey page where you want to generate a random number, click Add New: Action.
  2. Find Hidden Value in the list of actions and click the corresponding +Add button.
  3. Give your Hidden Value a Name and using the Populate with a calculated value dropdown menu, select A randomly generated number.
  4. Provide Range Start and Range End values. The larger the range, the better the likelihood that a number will not be repeated for multiple respondents.
    Hidden Value Primary Setup
  5. Make sure to Save Action when you are finished.

A random number within your range will now be generated for each respondent that lands on the page where you have added your Hidden Value Action.

Keep in mind that the number that is generated is truly random. As such, it is possible that the same number is generated for multiple respondents. To mitigate against this, consider a larger range. While a larger range does not guarantee that the same number is not generated twice, it does make this less likely.

If you truly need a unique number for each respondent, explore the Survey Counter Merge Code.

Generate a Random Number with a Custom Script Action

If you are comfortable with writing scripts (or have access to development resources), Alchemer's Custom Script Action can be utilized to generate random numbers.

The following Lua script example uses the math.random function to generate a random number between 0 and 1000. This number is then output onto the survey page:

random = math.random(0,1000)
print(random)

Visit the scripting resources to see the available Math Functions.

Scripting and Other Custom Solutions

We’re always happy to help you debug any documented script that is used as is. That said, we do not have the resources to write scripts on demand or to debug a customized script.

If you have customization ideas that you haven't figured out how to tackle, we're happy to be a sounding board for Alchemer features and functionality ideas that might meet your needs. Beyond this, check out our Professional Services; these folks have the scripting chops to help you to achieve what you are looking for!

Basic Standard Market Research HR Professional Full Access Reporting
Free Individual Team & Enterprise
Feature Included In