KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > brl > random


1 package gnu.brl;
2
3 // read.java -- procedure to read a BRL expression
4
// Copyright (C) 2000 Bruce R. Lewis and Eaton Vance Management
5
// See the file COPYING for license terms.
6

7 import kawa.lang.*;
8 import gnu.mapping.Procedure1;
9 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 JavaDoc prg = new java.util.Random JavaDoc();
16
17     public final Object JavaDoc apply1 (Object JavaDoc arg1)
18     {
19     IntNum retval = new IntNum();
20
21     // When passed an integer N, return an int M s.t. 0 <= M < N.
22
if (arg1 instanceof IntNum)
23         {
24         IntNum.divide(IntNum.makeU(Math.abs(prg.nextLong()) >> 1),
25                   (IntNum) arg1, null, retval, Numeric.FLOOR);
26         return retval;
27         }
28
29     // InPort, e.g. /dev/random, for initializing generator
30
if (arg1 instanceof InPort)
31         try
32         {
33             InPort i = (InPort)arg1;
34             prg.setSeed((long)
35                 i.read()
36                 + i.read() << 8
37                 + i.read() << 16
38                 + i.read() << 24);
39             i.close();
40             return retval;
41         }
42         catch (java.io.IOException JavaDoc e)
43         {
44             throw new GenericError ("I/O exception in brl-random: "
45                         + e.toString ());
46         }
47     throw new WrongType (this, 1, arg1, "real");
48     }
49 }
50
Popular Tags