001    /**
002     * Implements the CFML Function rand
003     */
004    package railo.runtime.functions.math;
005    
006    import java.security.NoSuchAlgorithmException;
007    import java.security.SecureRandom;
008    
009    import railo.commons.lang.StringUtil;
010    import railo.runtime.PageContext;
011    import railo.runtime.exp.ExpressionException;
012    import railo.runtime.ext.function.Function;
013    
014    public final class Rand implements Function {
015            
016            public static double call(PageContext pc )  {
017                    return StrictMath.random();
018            }
019            public static double call(PageContext pc , String algorithm) throws ExpressionException {
020                    algorithm=StringUtil.toLowerCase(algorithm);
021                    if("cfmx_compat".equals(algorithm)) { // TODO install  IBMSecureRandom to support it
022                            return StrictMath.random();
023                    }
024                    try {
025                            return SecureRandom.getInstance(algorithm).nextDouble();
026                    } 
027                    catch (NoSuchAlgorithmException e) {
028                            throw new ExpressionException("random algorithm ["+algorithm+"] is not installed on the system",e.getMessage());
029                    }       
030            }
031    }