001    /**
002     * Implements the CFML Function randomize
003     */
004    package railo.runtime.functions.math;
005    
006    import java.security.NoSuchAlgorithmException;
007    import java.security.SecureRandom;
008    import java.util.Random;
009    
010    import railo.commons.lang.StringUtil;
011    import railo.runtime.PageContext;
012    import railo.runtime.exp.ExpressionException;
013    import railo.runtime.ext.function.Function;
014    
015    public final class Randomize implements Function {
016            
017            private static Random simpleRandom=null;
018            
019            public static double call(PageContext pc , double number) {
020                    return new Random((long)number).nextDouble();
021            }
022            public static double call(PageContext pc , double number, String algorithm) throws ExpressionException {
023                    algorithm=StringUtil.toLowerCase(algorithm);
024                    if("cfmx_compat".equals(algorithm)) {
025                            return new Random((long)number).nextDouble();
026                    }
027                    
028                    try {
029                            SecureRandom secRandom = SecureRandom.getInstance(algorithm);
030                            secRandom.setSeed((long)number);
031                            return secRandom.nextDouble();
032                    } catch (NoSuchAlgorithmException e) {
033                            throw new ExpressionException("random algorithm ["+algorithm+"] is not installed on the system",e.getMessage());
034                    }       
035                    //return new Random((long)number).nextDouble();
036            }
037            
038    }