KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > lisp > RandomState


1 /*
2  * RandomState.java
3  *
4  * Copyright (C) 2003-2004 Peter Graves
5  * $Id: RandomState.java,v 1.2 2004/06/11 14:43:29 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */

21
22 package org.armedbear.lisp;
23
24 import java.io.File JavaDoc;
25 import java.io.FileInputStream JavaDoc;
26 import java.io.FileOutputStream JavaDoc;
27 import java.io.ObjectInputStream JavaDoc;
28 import java.io.ObjectOutputStream JavaDoc;
29 import java.math.BigInteger JavaDoc;
30 import java.util.Random JavaDoc;
31
32 public final class RandomState extends LispObject
33 {
34     private Random JavaDoc random;
35
36     public RandomState()
37     {
38         random = new Random JavaDoc();
39     }
40
41     public RandomState(RandomState rs) throws ConditionThrowable
42     {
43         try {
44             File JavaDoc file = File.createTempFile("MAKE-RANDOM-STATE", null);
45             FileOutputStream JavaDoc fileOut = new FileOutputStream JavaDoc(file);
46             ObjectOutputStream JavaDoc out = new ObjectOutputStream JavaDoc(fileOut);
47             out.writeObject(rs.random);
48             out.close();
49             FileInputStream JavaDoc fileIn = new FileInputStream JavaDoc(file);
50             ObjectInputStream JavaDoc in = new ObjectInputStream JavaDoc(fileIn);
51             random = (Random JavaDoc) in.readObject();
52             in.close();
53             file.delete();
54         }
55         catch (Throwable JavaDoc t) {
56             signal(new LispError("Unable to copy random state."));
57         }
58     }
59
60     public LispObject typeOf()
61     {
62         return Symbol.RANDOM_STATE;
63     }
64
65     public LispClass classOf()
66     {
67         return BuiltInClass.RANDOM_STATE;
68     }
69
70     public LispObject typep(LispObject type) throws ConditionThrowable
71     {
72         if (type == Symbol.RANDOM_STATE)
73             return T;
74         if (type == BuiltInClass.RANDOM_STATE)
75             return T;
76         return super.typep(type);
77     }
78
79     public String JavaDoc writeToString()
80     {
81         return unreadableString("RANDOM-STATE");
82     }
83
84     public LispObject random(LispObject arg) throws ConditionThrowable
85     {
86         if (arg instanceof Fixnum) {
87             int limit = ((Fixnum)arg).getValue();
88             if (limit > 0) {
89                 int n = random.nextInt((int)limit);
90                 return new Fixnum(n);
91             }
92         } else if (arg instanceof Bignum) {
93             BigInteger JavaDoc limit = ((Bignum)arg).getValue();
94             if (limit.signum() > 0) {
95                 int bitLength = limit.bitLength();
96                 BigInteger JavaDoc rand = new BigInteger JavaDoc(bitLength + 1, random);
97                 BigInteger JavaDoc remainder = rand.remainder(limit);
98                 return number(remainder);
99             }
100         } else if (arg instanceof LispFloat) {
101             double limit = ((LispFloat)arg).getValue();
102             if (limit > 0) {
103                 double rand = random.nextDouble();
104                 return new LispFloat(rand * limit);
105             }
106         }
107         return signal(new TypeError(arg, "positive integer or positive float"));
108     }
109
110     // ### random
111
// random limit &optional random-state => random-number
112
private static final Primitive RANDOM =
113         new Primitive("random", "limit &optional random-state")
114     {
115         public LispObject execute(LispObject arg) throws ConditionThrowable
116         {
117             RandomState randomState =
118                 (RandomState) _RANDOM_STATE_.symbolValueNoThrow();
119             return randomState.random(arg);
120         }
121         public LispObject execute(LispObject first, LispObject second)
122             throws ConditionThrowable
123         {
124             if (second instanceof RandomState) {
125                 RandomState randomState = (RandomState) second;
126                 return randomState.random(first);
127             }
128             return signal(new TypeError(first, Symbol.RANDOM_STATE));
129         }
130     };
131
132     // ### make-random-state
133
// make-random-state &optional state
134
private static final Primitive MAKE_RANDOM_STATE =
135         new Primitive("make-random-state", "&optional state") {
136         public LispObject execute() throws ConditionThrowable
137         {
138             return new RandomState((RandomState)_RANDOM_STATE_.symbolValueNoThrow());
139         }
140         public LispObject execute(LispObject arg)
141             throws ConditionThrowable
142         {
143             if (arg == NIL)
144                 return new RandomState((RandomState)_RANDOM_STATE_.symbolValueNoThrow());
145             if (arg == T)
146                 return new RandomState();
147             if (arg instanceof RandomState)
148                 return new RandomState((RandomState)arg);
149             return signal(new TypeError(arg, Symbol.RANDOM_STATE));
150         }
151     };
152
153     // ### random-state-p
154
private static final Primitive1 RANDOM_STATE_P =
155         new Primitive1("random-state-p", "object")
156     {
157         public LispObject execute(LispObject arg)
158         {
159             return arg instanceof RandomState ? T : NIL;
160         }
161     };
162 }
163
Popular Tags