Rand

Description

Generates a pseudo-random number.

Returns

A pseudo-random decimal number, in the range 0-1.

Category

Syntax

Rand([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

Parameters

Parameter

Description

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 than the default algorithm. (Default)
  • IBMSecureRandom: for IBM WebSphere (IBM JVM does not support the SHA1PRNG algorithm).

Usage

Call the Randomize function before calling this function to seed the random number generator. Seeding the generator ensures that the Rand function always generates the same sequence of pseudo-random numbers. This behavior is useful if you must reproduce a pattern consistently. 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 uses all three algorithms to generate a single random number.

<cfscript>
       randAlgorithmArray=["CFMX_COMPAT","SHA1PRNG","IBMSecureRandom"];
       for (index=1;index<=arrayLen(randAlgorithmArray);index++){
             WriteOutput("The random value using #randAlgorithmArray[index]# is: " & rand(randAlgorithmArray[index]) & " | ");
       }
</cfscript>

Output

The random value using CFMX_COMPAT is: 0.329643036436 | The random value using SHA1PRNG is: 0.383554386687 | The random value using IBMSecureRandom is: 0.429820155151 |

 Adobe

Get help faster and easier

New user?