01: package gnu.brl;
02:
03: // read.java -- procedure to read a BRL expression
04: // Copyright (C) 2000 Bruce R. Lewis and Eaton Vance Management
05: // See the file COPYING for license terms.
06:
07: import kawa.lang.*;
08: import gnu.mapping.Procedure1;
09: import gnu.mapping.WrongType;
10: import gnu.mapping.InPort;
11: import gnu.math.*;
12:
13: public class random extends Procedure1 {
14:
15: static java.util.Random prg = new java.util.Random();
16:
17: public final Object apply1(Object arg1) {
18: IntNum retval = new IntNum();
19:
20: // When passed an integer N, return an int M s.t. 0 <= M < N.
21: if (arg1 instanceof IntNum) {
22: IntNum.divide(IntNum.makeU(Math.abs(prg.nextLong()) >> 1),
23: (IntNum) arg1, null, retval, Numeric.FLOOR);
24: return retval;
25: }
26:
27: // InPort, e.g. /dev/random, for initializing generator
28: if (arg1 instanceof InPort)
29: try {
30: InPort i = (InPort) arg1;
31: prg
32: .setSeed((long) i.read() + i.read() << 8 + i
33: .read() << 16 + i.read() << 24);
34: i.close();
35: return retval;
36: } catch (java.io.IOException e) {
37: throw new GenericError("I/O exception in brl-random: "
38: + e.toString());
39: }
40: throw new WrongType(this , 1, arg1, "real");
41: }
42: }
|