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

Last change on this file was 12254, checked in by ehuelsmann, 16 years ago

Remove 'throws ConditionThrowable?' method annotations:

it's an unchecked exception now, so no need to declare it thrown.

  • 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 12254 2009-11-06 20:07:54Z 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 java.io.File;
37import java.io.FileInputStream;
38import java.io.FileOutputStream;
39import java.io.ObjectInputStream;
40import java.io.ObjectOutputStream;
41import java.math.BigInteger;
42import java.util.Random;
43
44public final class RandomState extends LispObject
45{
46    private Random random;
47
48    public RandomState()
49    {
50        random = new Random();
51    }
52
53    public RandomState(RandomState rs)
54    {
55        try {
56            File file = File.createTempFile("MAKE-RANDOM-STATE", null);
57            FileOutputStream fileOut = new FileOutputStream(file);
58            ObjectOutputStream out = new ObjectOutputStream(fileOut);
59            out.writeObject(rs.random);
60            out.close();
61            FileInputStream fileIn = new FileInputStream(file);
62            ObjectInputStream in = new ObjectInputStream(fileIn);
63            random = (Random) in.readObject();
64            in.close();
65            file.delete();
66        }
67        catch (Throwable t) {
68            error(new LispError("Unable to copy random state."));
69        }
70    }
71
72    @Override
73    public LispObject typeOf()
74    {
75        return Symbol.RANDOM_STATE;
76    }
77
78    @Override
79    public LispObject classOf()
80    {
81        return BuiltInClass.RANDOM_STATE;
82    }
83
84    @Override
85    public LispObject typep(LispObject type)
86    {
87        if (type == Symbol.RANDOM_STATE)
88            return T;
89        if (type == BuiltInClass.RANDOM_STATE)
90            return T;
91        return super.typep(type);
92    }
93
94    @Override
95    public String writeToString()
96    {
97        return unreadableString(Symbol.RANDOM_STATE);
98    }
99
100    public LispObject random(LispObject arg)
101    {
102        if (arg instanceof Fixnum) {
103            int limit = ((Fixnum)arg).value;
104            if (limit > 0) {
105                int n = random.nextInt((int)limit);
106                return Fixnum.getInstance(n);
107            }
108        } else if (arg instanceof Bignum) {
109            BigInteger limit = ((Bignum)arg).value;
110            if (limit.signum() > 0) {
111                int bitLength = limit.bitLength();
112                BigInteger rand = new BigInteger(bitLength + 1, random);
113                BigInteger remainder = rand.remainder(limit);
114                return number(remainder);
115            }
116        } else if (arg instanceof SingleFloat) {
117            float limit = ((SingleFloat)arg).value;
118            if (limit > 0) {
119                float rand = random.nextFloat();
120                return new SingleFloat(rand * limit);
121            }
122        } else if (arg instanceof DoubleFloat) {
123            double limit = ((DoubleFloat)arg).value;
124            if (limit > 0) {
125                double rand = random.nextDouble();
126                return new DoubleFloat(rand * limit);
127            }
128        }
129        return type_error(arg, list(Symbol.OR,
130                                          list(Symbol.INTEGER, Fixnum.ONE),
131                                          list(Symbol.FLOAT, list(Fixnum.ZERO))));
132    }
133
134    // ### random limit &optional random-state => random-number
135    private static final Primitive RANDOM =
136        new Primitive(Symbol.RANDOM, "limit &optional random-state")
137    {
138        @Override
139        public LispObject execute(LispObject arg)
140        {
141            RandomState randomState =
142                (RandomState) Symbol._RANDOM_STATE_.symbolValue();
143            return randomState.random(arg);
144        }
145        @Override
146        public LispObject execute(LispObject first, LispObject second)
147
148        {
149            if (second instanceof RandomState) {
150                RandomState randomState = (RandomState) second;
151                return randomState.random(first);
152            }
153            return type_error(first, Symbol.RANDOM_STATE);
154        }
155    };
156
157    // ### make-random-state &optional state
158    private static final Primitive MAKE_RANDOM_STATE =
159        new Primitive(Symbol.MAKE_RANDOM_STATE, "&optional state")
160    {
161        @Override
162        public LispObject execute()
163        {
164            return new RandomState((RandomState)Symbol._RANDOM_STATE_.symbolValue());
165        }
166        @Override
167        public LispObject execute(LispObject arg)
168
169        {
170            if (arg == NIL)
171                return new RandomState((RandomState)Symbol._RANDOM_STATE_.symbolValue());
172            if (arg == T)
173                return new RandomState();
174            if (arg instanceof RandomState)
175                return new RandomState((RandomState)arg);
176            return type_error(arg, Symbol.RANDOM_STATE);
177        }
178    };
179
180    // ### random-state-p
181    private static final Primitive RANDOM_STATE_P =
182        new Primitive(Symbol.RANDOM_STATE_P, "object")
183    {
184        @Override
185        public LispObject execute(LispObject arg)
186        {
187            return arg instanceof RandomState ? T : NIL;
188        }
189    };
190}
Note: See TracBrowser for help on using the repository browser.