Randomize

Description

Seeds the pseudo-random number generator with an integer number, ensuring repeatable number patterns.

Returns

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

Function syntax

Randomize(number[, algorithm])

History

ColdFusion MX 7: Added the algorithm parameter.

See also

Rand, RandRange

Parameters

Parameter

Description

number

Integer number. If the number is 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 seed number. ColdFusion installs a cryptography library with the following algorithms:

  • CFMX_COMPAT: the algorithm used in ColdFusion (default).

  • SHA1PRNG: generates a number using the Sun Java SHA1PRNG algorithm. This algorithm provides greater randomness than the default algorithm.

  • IBMSecureRandom: for IBM WebSphere (IBM JVM does not support the SHA1PRNG algorithm).

Usage

Call this function before calling Rand 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.

In Standard Edition, for all algorithms except the default algorithm, ColdFusion uses the Java Cryptography Extension (JCE) and installs a Sun Java runtime that includes the Sun JCE default security provider. This provider includes the algorithms listed in the Parameters section. The JCE framework includes facilities for using other provider implementations; however, Adobe cannot provide technical support for third-party security providers.

In Enterprise Edition, ColdFusion also installs the RSA BSafe Crypto-J library. This provider adds the following algorithms: FIPS186PRNG, MD5PRNG, DummyPRNG, OBFPRNG. DummyPRNG always returns 0.

Example

The following example calls the Randomize function to seed the random number generator and generates 10 random numbers. To show the effect of the seed, submit the form with the same value multiple times.

<h3>Randomize Example</h3> 
 
    <!--- Do the following only if the form has been submitted. ---> 
<cfif IsDefined("Form.myRandomInt")> 
 
    <!--- Make sure submitted value is a number and display its value. ---> 
    <cfif IsNumeric(FORM.myRandomInt)> 
        <cfoutput> 
            <b>Seed value is #FORM.myRandomInt#</b><br> 
        </cfoutput><br> 
 
        <!--- Call Randomize to seed the random number generator. ---> 
        <cfset r = Randomize(FORM.myRandomInt, "SHA1PRNG")> 
 
        <cfoutput> 
            <b>Random number returned by Randomize(#Form.myRandomInt#, 
                    "SHA1PRNG"):</b><br> 
            #r#<br> 
            <br> 
            <b>10 random numbers generated using the SHA1PRNG algorithm:</b><br> 
            <cfloop index = "i" from = "1" to = "10" step = "1"> 
                #Rand("SHA1PRNG")#<br> 
            </cfloop><br> 
        </cfoutput> 
 
    <cfelse> 
        <p>Please enter a number. 
    </cfif> 
</cfif> 
 
<!--- Form to specify the seed value. ---> 
<form action="#CGI.SCRIPT_NAME#" method="post"> 
<p>Enter a number to seed the randomizer: 
<input type = "Text" name = "MyRandomInt" value="12345"> 
<p><input type = "Submit" name = ""> 
</form>