01: /*
02: * @(#)random.java 1.2 04/12/06
03: *
04: * Copyright (c) 1997-2004 Sun Microsystems, Inc. All Rights Reserved.
05: *
06: * See the file "LICENSE.txt" for information on usage and redistribution
07: * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
08: */
09: package org.pnuts.lib;
10:
11: import pnuts.lang.*;
12: import java.util.Random;
13:
14: public class random extends PnutsFunction {
15:
16: final static String CONTEXT_SYMBOL = "pnuts.lib.random".intern();
17:
18: public random() {
19: super ("random");
20: }
21:
22: public boolean defined(int narg) {
23: return (narg == 0 || narg == 1);
24: }
25:
26: protected Object exec(Object[] args, Context context) {
27: int nargs = args.length;
28: if (nargs == 0) {
29: Random r = (Random) context.get(CONTEXT_SYMBOL);
30: if (r == null) {
31: context.set(CONTEXT_SYMBOL, r = new Random());
32: }
33: return new Integer(r.nextInt());
34: } else if (nargs == 1) {
35: return new Integer(((Random) args[0]).nextInt());
36: } else {
37: undefined(args, context);
38: return null;
39: }
40: }
41:
42: public String toString() {
43: return "function random( { java.util.Random } )";
44: }
45: }
|