Updated on 2025-01-10 GMT+08:00

Generating a Random Decimal in a Specified Range

Function Name

$randomDecimal(double A, double B, int C)

Function Description

Generates a random decimal with C decimal places within the range between parameter A and parameter B. CodeArts TestPlan can generate random decimals with up to 9 decimal places, within the range of [–9999999999.999999999, +9999999999.999999999].

Parameters A and B support the following types:

  • Decimals or integers
  • Environment parameters
  • Local parameters
  • Other built-in functions

Parameter C supports the following types:

  • Positive integers
  • Environment parameters
  • Local parameters
  • Other built-in functions

If C is 0, the function generates a random integer.

Application Scenarios

The function for generating random decimals within a specified range can be used in the following scenarios for API automation:

  • Request URL
  • Request header
  • Request body
  • Checkpoint property
  • if condition
  • for loop interrupt condition

Example

  • Request URL

    As shown in the following figure, the value of test in the request URL is the function for generating random decimals within a specified range. Parameter A in the function is 1, parameter B is 100, and parameter C is 2.

  • Request header

    As shown in the following figure, the value of number in the request header is the function for generating random decimals within a specified range. Parameter A in the function is 1, parameter B is 100, and parameter C is 2.

  • Request body

    As shown in the following figure, the request body uses the function for generating random decimals within a specified range. Parameter A in the function is the binary addition operation $add(1,1). Parameter B is the binary multiplication operation $multiply(10,10).

    Parameter C is 2.

  • Checkpoint property

    As shown in the following figure, the target value of the checkpoint property result is the function for generating random decimals within a specified range. Parameter A in the function is 1, parameter B is the local parameter test, and parameter C is 2. For details about how to set local parameters, see Local Parameters.

  • if condition

    As shown in the following figure, the target value of the if condition is the function for generating random decimals within a specified range. Parameter A in the function is 1, parameter B is the environment variable status, and parameter C is 2. For details about how to set environment parameters, see Setting Environment Parameters of an API Script.

  • for loop interrupt condition

    As shown in the following figure, the target value of the for loop interrupt condition is the function for generating random decimals within a specified range. Parameter A in the function is 1, parameter B is 100, and parameter C is 2.