source: branches/1.1.x/src/org/armedbear/lisp/RandomState.java

Last change on this file was 13444, checked in by ehuelsmann, 13 years ago

Reduce the number of required unreadableString() methods by removing
the ones taking a symbol argument: all invocations involved a constant
parameter, now replaced with a constant string instead.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 6.2 KB
Line 
1/*
2 * RandomState.java
3 *
4 * Copyright (C) 2003-2005 Peter Graves
5 * $Id: RandomState.java 13444 2011-08-06 13:51:26Z ehuelsmann $
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
34package org.armedbear.lisp;
35
36import static org.armedbear.lisp.Lisp.*;
37
38import java.io.ByteArrayInputStream;
39import java.io.ByteArrayOutputStream;
40import java.io.ObjectInputStream;
41import java.io.ObjectOutputStream;
42import java.math.BigInteger;
43import java.util.Random;
44
45public 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    @Override
72    public LispObject typeOf()
73    {
74        return Symbol.RANDOM_STATE;
75    }
76
77    @Override
78    public LispObject classOf()
79    {
80        return BuiltInClass.RANDOM_STATE;
81    }
82
83    @Override
84    public LispObject typep(LispObject type)
85    {
86        if (type == Symbol.RANDOM_STATE)
87            return T;
88        if (type == BuiltInClass.RANDOM_STATE)
89            return T;
90        return super.typep(type);
91    }
92
93    @Override
94    public String printObject()
95    {
96        return unreadableString("RANDOM-STATE");
97    }
98
99    public LispObject random(LispObject arg)
100    {
101        if (arg instanceof Fixnum) {
102            int limit = ((Fixnum)arg).value;
103            if (limit > 0) {
104                int n = random.nextInt((int)limit);
105                return Fixnum.getInstance(n);
106            }
107        } else if (arg instanceof Bignum) {
108            BigInteger limit = ((Bignum)arg).value;
109            if (limit.signum() > 0) {
110                int bitLength = limit.bitLength();
111                BigInteger rand = new BigInteger(bitLength + 1, random);
112                BigInteger remainder = rand.remainder(limit);
113                return number(remainder);
114            }
115        } else if (arg instanceof SingleFloat) {
116            float limit = ((SingleFloat)arg).value;
117            if (limit > 0) {
118                float rand = random.nextFloat();
119                return new SingleFloat(rand * limit);
120            }
121        } else if (arg instanceof DoubleFloat) {
122            double limit = ((DoubleFloat)arg).value;
123            if (limit > 0) {
124                double rand = random.nextDouble();
125                return new DoubleFloat(rand * limit);
126            }
127        }
128        return type_error(arg, list(Symbol.OR,
129                                          list(Symbol.INTEGER, Fixnum.ONE),
130                                          list(Symbol.FLOAT, list(Fixnum.ZERO))));
131    }
132
133    // ### random limit &optional random-state => random-number
134    private static final Primitive RANDOM =
135        new Primitive(Symbol.RANDOM, "limit &optional random-state")
136    {
137        @Override
138        public LispObject execute(LispObject arg)
139        {
140            RandomState randomState =
141                (RandomState) Symbol._RANDOM_STATE_.symbolValue();
142            return randomState.random(arg);
143        }
144        @Override
145        public LispObject execute(LispObject first, LispObject second)
146
147        {
148            if (second instanceof RandomState) {
149                RandomState randomState = (RandomState) second;
150                return randomState.random(first);
151            }
152            return type_error(first, Symbol.RANDOM_STATE);
153        }
154    };
155
156    // ### make-random-state &optional state
157    private static final Primitive MAKE_RANDOM_STATE =
158        new Primitive(Symbol.MAKE_RANDOM_STATE, "&optional state")
159    {
160        @Override
161        public LispObject execute()
162        {
163            return new RandomState((RandomState)Symbol._RANDOM_STATE_.symbolValue());
164        }
165        @Override
166        public LispObject execute(LispObject arg)
167
168        {
169            if (arg == NIL)
170                return new RandomState((RandomState)Symbol._RANDOM_STATE_.symbolValue());
171            if (arg == T)
172                return new RandomState();
173            if (arg instanceof RandomState)
174                return new RandomState((RandomState)arg);
175            return type_error(arg, Symbol.RANDOM_STATE);
176        }
177    };
178
179    // ### random-state-p
180    private static final Primitive RANDOM_STATE_P =
181        new Primitive(Symbol.RANDOM_STATE_P, "object")
182    {
183        @Override
184        public LispObject execute(LispObject arg)
185        {
186            return arg instanceof RandomState ? T : NIL;
187        }
188    };
189}
Note: See TracBrowser for help on using the repository browser.