# Generating a Random Number

You are viewing an old version of this page. View the current version.

Version 3

# Overview

random

• Return:  Returns a random number between 0 and 1 (ex. 0.738).
• Arguments: None
• Usage: random()
• Example Usage: When you need to generate a random number.
• For example, to generate a number between 5 and 23, you can use (random()*(23 - 5)) + 5.  This will be something like 12.43334.
• You can convert that to a whole number by using int((random()*(23 - 5)) + 5).  You can also reference questions instead of directly typing numbers.
• Ex. int(random()*(#form/high_num - #form/low_num) + #form/low_num).

You can generate either a random decimal number (ex. 8.9) or a random integer (ex. 23) using the random() function.  Set the calculate expression of your question to the following:

1. For a random decimal: start_num + (random() * (end_num - start_num)) For example, to generate random number between 10.3 and 30.9, you can use 10.3 + (random() * (30.9 - 10.3))
2. For a random integer: start_num + int(random() * (end_num - start_num)) For example, to generate random number between 9 and 22, you can use 9 + int(random() * (22 - 9))

You can also replace start_num and end_num with references to other questions in your form (ex. #form/my_start_range_question).  Please make sure that they are required or have value so that the calculate expression works.

• No labels