All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.scudata.expression.fn.math.Rand Maven / Gradle / Ivy

Go to download

SPL(Structured Process Language) A programming language specially for structured data computing.

There is a newer version: 20241126
Show newest version
package com.scudata.expression.fn.math;

import com.scudata.common.MessageManager;
import com.scudata.common.RQException;
import com.scudata.dm.Context;
import com.scudata.expression.Function;
import com.scudata.expression.Node;
import com.scudata.resources.EngineMessage;

/**
 * rand(n)	????С??n???????????nʡ???򷵻?[0,1]????????С??
	@s	?趨?????????
 *
 */
public class Rand extends Function {
	public Node optimize(Context ctx) {
		if (param != null) {
			param.optimize(ctx);
		}
		
		return this;
	}

	public Object calculate(Context ctx) {
		if (param == null) {//????[0,1]????????С??
			return new Double(ctx.getRandom().nextDouble());
		} else if (param.isLeaf()) {
			Object obj = param.getLeafExpression().calculate(ctx);
			if (!(obj instanceof Number)) {
				MessageManager mm = EngineMessage.get();
				throw new RQException("rand" + mm.getMessage("function.paramTypeError"));
			}

			if (option == null || option.indexOf('s') == -1) {//????С??n?????????
				int n = ((Number)obj).intValue();
				return new Integer(ctx.getRandom().nextInt(n));
			} else {//?趨?????????
				long seed = ((Number)obj).longValue();
				ctx.getRandom(seed);
				return null;
			}
		} else {
			MessageManager mm = EngineMessage.get();
			throw new RQException("rand" + mm.getMessage("function.invalidParam"));
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy