- Return: Returns a random number between 0.0 (inclusive) and 1.0 (exclusive) (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 (inclusive) and 23 (exclusive), 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).
- The output varies between low_num (inclusive) and high_num (exclusive)
You can generate either a random decimal number (ex. 8.9) or a random integer (ex. 22) using the random() function. Set the calculate expression of your question to the following:
- 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))
- 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.
Notes on the random() Function:
- If random() is used within a calculate or display condition, the value will change and be recalculated many time as the form is filled out
- To generate a random number once when the form is opened, add a hidden value and then put the random() logic inside the Default Value logic of the hidden value (its in Advanced Section).