1 | /* |
---|
2 | * RandomState.java |
---|
3 | * |
---|
4 | * Copyright (C) 2003-2005 Peter Graves |
---|
5 | * $Id: RandomState.java 15500 2021-01-29 15:07:17Z mevenson $ |
---|
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 | * As a special exception, the copyright holders of this library give you |
---|
22 | * permission to link this library with independent modules to produce an |
---|
23 | * executable, regardless of the license terms of these independent |
---|
24 | * modules, and to copy and distribute the resulting executable under |
---|
25 | * terms of your choice, provided that you also meet, for each linked |
---|
26 | * independent module, the terms and conditions of the license of that |
---|
27 | * module. An independent module is a module which is not derived from |
---|
28 | * or based on this library. If you modify this library, you may extend |
---|
29 | * this exception to your version of the library, but you are not |
---|
30 | * obligated to do so. If you do not wish to do so, delete this |
---|
31 | * exception statement from your version. |
---|
32 | */ |
---|
33 | |
---|
34 | package org.armedbear.lisp; |
---|
35 | |
---|
36 | import static org.armedbear.lisp.Lisp.*; |
---|
37 | |
---|
38 | import java.io.ByteArrayInputStream; |
---|
39 | import java.io.ByteArrayOutputStream; |
---|
40 | import java.io.ObjectInputStream; |
---|
41 | import java.io.ObjectOutputStream; |
---|
42 | import java.math.BigInteger; |
---|
43 | import java.util.Random; |
---|
44 | |
---|
45 | public final class RandomState extends LispObject |
---|
46 | { |
---|
47 | private Random random; |
---|
48 | |
---|
49 | public RandomState() |
---|
50 | { |
---|
51 | random = new Random(); |
---|
52 | } |
---|
53 | |
---|
54 | public RandomState(RandomState rs) |
---|
55 | { |
---|
56 | try { |
---|
57 | ByteArrayOutputStream byteOut = new ByteArrayOutputStream(); |
---|
58 | ObjectOutputStream out = new ObjectOutputStream(byteOut); |
---|
59 | out.writeObject(rs.random); |
---|
60 | out.close(); |
---|
61 | ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut.toByteArray()); |
---|
62 | ObjectInputStream in = new ObjectInputStream(byteIn); |
---|
63 | random = (Random) in.readObject(); |
---|
64 | in.close(); |
---|
65 | } |
---|
66 | catch (Throwable t) { // ANY exception gets converted to a lisp error |
---|
67 | error(new LispError("Unable to copy random state.")); |
---|
68 | } |
---|
69 | } |
---|
70 | |
---|
71 | public RandomState(SimpleVector v) |
---|
72 | { |
---|
73 | int length = v.capacity; |
---|
74 | byte[] bytes = new byte[length]; |
---|
75 | for (int i = 0; i < length; ++i) { |
---|
76 | LispObject obj = v.data[i]; |
---|
77 | if (obj instanceof Fixnum) { |
---|
78 | bytes[i] = (byte)((Fixnum)obj).value; |
---|
79 | } else { |
---|
80 | error(type_error(obj, Symbol.FIXNUM)); |
---|
81 | } |
---|
82 | } |
---|
83 | try { |
---|
84 | ByteArrayInputStream byteIn = new ByteArrayInputStream(bytes); |
---|
85 | ObjectInputStream in = new ObjectInputStream(byteIn); |
---|
86 | random = (Random) in.readObject(); |
---|
87 | in.close(); |
---|
88 | } |
---|
89 | catch (Throwable t) { // ANY exception gets converted to a lisp error |
---|
90 | error(new LispError("Unable to read random state.")); |
---|
91 | } |
---|
92 | } |
---|
93 | |
---|
94 | @Override |
---|
95 | public LispObject typeOf() |
---|
96 | { |
---|
97 | return Symbol.RANDOM_STATE; |
---|
98 | } |
---|
99 | |
---|
100 | @Override |
---|
101 | public LispObject classOf() |
---|
102 | { |
---|
103 | return BuiltInClass.RANDOM_STATE; |
---|
104 | } |
---|
105 | |
---|
106 | @Override |
---|
107 | public LispObject typep(LispObject type) |
---|
108 | { |
---|
109 | if (type == Symbol.RANDOM_STATE) |
---|
110 | return T; |
---|
111 | if (type == BuiltInClass.RANDOM_STATE) |
---|
112 | return T; |
---|
113 | return super.typep(type); |
---|
114 | } |
---|
115 | |
---|
116 | @Override |
---|
117 | public String printObject() |
---|
118 | { |
---|
119 | LispThread thread = LispThread.currentThread(); |
---|
120 | StringBuilder sb = new StringBuilder(); |
---|
121 | if (Symbol.PRINT_READABLY.symbolValue(thread) != NIL) { |
---|
122 | // we need the #. reader macro for now |
---|
123 | if (Symbol.READ_EVAL.symbolValue(thread) == NIL) { |
---|
124 | error(new PrintNotReadable(list(Keyword.OBJECT, this))); |
---|
125 | // Not reached. |
---|
126 | return null; |
---|
127 | } |
---|
128 | int base = Fixnum.getValue(Symbol.PRINT_BASE.symbolValue(thread)); |
---|
129 | ByteArrayOutputStream byteOut = new ByteArrayOutputStream(); |
---|
130 | try { |
---|
131 | sb.append("#.(SYSTEM::READ-RANDOM-STATE #("); |
---|
132 | ObjectOutputStream out = new ObjectOutputStream(byteOut); |
---|
133 | out.writeObject(random); |
---|
134 | out.close(); |
---|
135 | } |
---|
136 | catch (Throwable t) { // ANY exception gets converted to a lisp error |
---|
137 | error(new LispError("Unable to copy random state.")); |
---|
138 | } |
---|
139 | byte[] bytes = byteOut.toByteArray(); |
---|
140 | for (int i = 0; i < bytes.length; ++i) { |
---|
141 | if (i != 0) { |
---|
142 | sb.append(" "); |
---|
143 | } |
---|
144 | sb.append(Integer.toString(bytes[i], base).toUpperCase()); |
---|
145 | } |
---|
146 | sb.append("))"); |
---|
147 | } else { |
---|
148 | return unreadableString("RANDOM-STATE"); |
---|
149 | } |
---|
150 | return sb.toString(); |
---|
151 | } |
---|
152 | |
---|
153 | public LispObject random(LispObject arg) |
---|
154 | { |
---|
155 | if (arg instanceof Fixnum) { |
---|
156 | int limit = ((Fixnum)arg).value; |
---|
157 | if (limit > 0) { |
---|
158 | int n = random.nextInt((int)limit); |
---|
159 | return Fixnum.getInstance(n); |
---|
160 | } |
---|
161 | } else if (arg instanceof Bignum) { |
---|
162 | BigInteger limit = ((Bignum)arg).value; |
---|
163 | if (limit.signum() > 0) { |
---|
164 | int bitLength = limit.bitLength(); |
---|
165 | BigInteger rand = new BigInteger(bitLength + 1, random); |
---|
166 | BigInteger remainder = rand.remainder(limit); |
---|
167 | return number(remainder); |
---|
168 | } |
---|
169 | } else if (arg instanceof SingleFloat) { |
---|
170 | float limit = ((SingleFloat)arg).value; |
---|
171 | if (limit > 0) { |
---|
172 | float rand = random.nextFloat(); |
---|
173 | return new SingleFloat(rand * limit); |
---|
174 | } |
---|
175 | } else if (arg instanceof DoubleFloat) { |
---|
176 | double limit = ((DoubleFloat)arg).value; |
---|
177 | if (limit > 0) { |
---|
178 | double rand = random.nextDouble(); |
---|
179 | return new DoubleFloat(rand * limit); |
---|
180 | } |
---|
181 | } |
---|
182 | return type_error(arg, list(Symbol.OR, |
---|
183 | list(Symbol.INTEGER, Fixnum.ONE), |
---|
184 | list(Symbol.FLOAT, list(Fixnum.ZERO)))); |
---|
185 | } |
---|
186 | |
---|
187 | // ### random limit &optional random-state => random-number |
---|
188 | private static final Primitive RANDOM = |
---|
189 | new Primitive(Symbol.RANDOM, "limit &optional random-state") |
---|
190 | { |
---|
191 | @Override |
---|
192 | public LispObject execute(LispObject arg) |
---|
193 | { |
---|
194 | RandomState randomState = |
---|
195 | (RandomState) Symbol._RANDOM_STATE_.symbolValue(); |
---|
196 | return randomState.random(arg); |
---|
197 | } |
---|
198 | @Override |
---|
199 | public LispObject execute(LispObject first, LispObject second) |
---|
200 | |
---|
201 | { |
---|
202 | if (second instanceof RandomState) { |
---|
203 | RandomState randomState = (RandomState) second; |
---|
204 | return randomState.random(first); |
---|
205 | } |
---|
206 | return type_error(first, Symbol.RANDOM_STATE); |
---|
207 | } |
---|
208 | }; |
---|
209 | |
---|
210 | // ### make-random-state &optional state |
---|
211 | private static final Primitive MAKE_RANDOM_STATE = |
---|
212 | new Primitive(Symbol.MAKE_RANDOM_STATE, "&optional state") |
---|
213 | { |
---|
214 | @Override |
---|
215 | public LispObject execute() |
---|
216 | { |
---|
217 | return new RandomState((RandomState)Symbol._RANDOM_STATE_.symbolValue()); |
---|
218 | } |
---|
219 | @Override |
---|
220 | public LispObject execute(LispObject arg) |
---|
221 | |
---|
222 | { |
---|
223 | if (arg == NIL) |
---|
224 | return new RandomState((RandomState)Symbol._RANDOM_STATE_.symbolValue()); |
---|
225 | if (arg == T) |
---|
226 | return new RandomState(); |
---|
227 | if (arg instanceof RandomState) |
---|
228 | return new RandomState((RandomState)arg); |
---|
229 | return type_error(arg, Symbol.RANDOM_STATE); |
---|
230 | } |
---|
231 | }; |
---|
232 | |
---|
233 | private static final Primitive READ_RANDOM_STATE = |
---|
234 | new Primitive(Symbol.READ_RANDOM_STATE, "state") |
---|
235 | { |
---|
236 | @Override |
---|
237 | public LispObject execute(LispObject arg) |
---|
238 | |
---|
239 | { |
---|
240 | if (arg instanceof SimpleVector) |
---|
241 | return new RandomState((SimpleVector)arg); |
---|
242 | return type_error(arg, Symbol.RANDOM_STATE); |
---|
243 | } |
---|
244 | }; |
---|
245 | |
---|
246 | // ### random-state-p |
---|
247 | private static final Primitive RANDOM_STATE_P = |
---|
248 | new Primitive(Symbol.RANDOM_STATE_P, "object") |
---|
249 | { |
---|
250 | @Override |
---|
251 | public LispObject execute(LispObject arg) |
---|
252 | { |
---|
253 | return arg instanceof RandomState ? T : NIL; |
---|
254 | } |
---|
255 | }; |
---|
256 | } |
---|