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