Updated on 2024-04-25 GMT+08:00

Generating a Random Number in a Specified Range

Function Name

$random_int(intA, intB)

Function Description

Generates a random number within the range between parameter A and parameter B. CodeArts TestPlan can generate random numbers containing a maximum of 10 digits, that is, the value range is [–9999999999, +9999999999].

Parameters A and B support the following types:

  • Numbers
  • Environment parameters
  • Local parameters
  • Other built-in functions

Application Scenarios

The function for generating random numbers 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 numbers within a specified range. Parameter A in the function is 1, and parameter B is 100.

  • Request header

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

  • Request body

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

  • Checkpoint property

    As shown in the following figure, the target value of the checkpoint property result is the function for generating random numbers within a specified range. Parameter A in the function is 1, and parameter B is the local parameter test. 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 numbers within a specified range. Parameter A in the function is 1, and parameter B is the environment variable status. For details about how to set environment parameters, see Setting Environment Parameters.

  • 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 numbers within a specified range. Parameter A in the function is 1, and parameter B is 100.