RandRange

Description

Generates a pseudo-random integer in the range between two specified numbers.

Returns

A pseudo-random integer.

Category

Mathematical functionsSecurity functions

Function syntax

RandRange(number1, number2[, algorithm])

History

  • ColdFusion (2023 release) Update 8 and ColdFusion (2021 release) Update 14: Changed the default algorithm from CFMX_COMPAT to SHA1PRNG.
  • ColdFusion MX 7: Added the algorithm parameter.

See also

RandRandomize

Parameters

Parameter

Description

number1, number2

Integer numbers. If the numbers are not in the range -2,147,483,648 - 2,147,483,647, ColdFusion generates an error.

algorithm

(Optional) The algorithm to use to generate the random number. ColdFusion installs a cryptography library with the following algorithms:

  • CFMX_COMPAT: the algorithm used in ColdFusion.
  • SHA1PRNG: generates a number using the Sun Java SHA1PRNG algorithm. This algorithm provides greater randomness. (Default)
  • IBMSecureRandom: for IBM WebSphere (IBM JVM does not support the SHA1PRNG algorithm.)

Usage

Very large positive or negative values for the number1 and number2 parameters might result in poor randomness in the results. To prevent this problem, do not specify numbers outside the range -1,000,000,000 - 1,000,000,000. ColdFusion uses the Java Cryptography Extension (JCE) and installs a Sun Java 1.4.2 runtime that includes the Sun JCE default security provider. This provider includes the algorithms listed in the Parameters section (except the default algorithm). The JCE framework includes facilities for using other provider implementations; however, cannot provide technical support for third-party security providers.

Example

The following example generates a random number in the range (-100000,100000) using all three algorithms.

<cfscript>
       num1 = -100000;
       num2 = 100000;
       randAlgorithmArray = ["CFMX_COMPAT", "SHA1PRNG", "IBMSecureRandom"];
       for(index = 1; index <= arrayLen(randAlgorithmArray); index++)
       {
             WriteOutput("The rand number in the range #num1# to #num2# using #randAlgorithmArray[index]# is: " 
             & randRange(num1, num2, randAlgorithmArray[index]) & "<br/>");
       }
</cfscript>

Output

The rand number in the range -100000 to 100000 using CFMX_COMPAT is: 73552
The rand number in the range -100000 to 100000 using SHA1PRNG is: 59144
The rand number in the range -100000 to 100000 using IBMSecureRandom is: 28588

 Adobe

Получайте помощь быстрее и проще

Новый пользователь?