annotate src/clojure/contrib/probabilities/random_numbers.clj @ 10:ef7dbbd6452c

added clojure source goodness
author Robert McIntyre <rlm@mit.edu>
date Sat, 21 Aug 2010 06:25:44 -0400
parents
children
rev   line source
rlm@10 1 ;; Random number generators
rlm@10 2
rlm@10 3 ;; by Konrad Hinsen
rlm@10 4 ;; last updated May 3, 2009
rlm@10 5
rlm@10 6 ;; Copyright (c) Konrad Hinsen, 2009. All rights reserved. The use
rlm@10 7 ;; and distribution terms for this software are covered by the Eclipse
rlm@10 8 ;; Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
rlm@10 9 ;; which can be found in the file epl-v10.html at the root of this
rlm@10 10 ;; distribution. By using this software in any fashion, you are
rlm@10 11 ;; agreeing to be bound by the terms of this license. You must not
rlm@10 12 ;; remove this notice, or any other, from this software.
rlm@10 13
rlm@10 14 (ns
rlm@10 15 ^{:author "Konrad Hinsen"
rlm@10 16 :doc "Random number streams
rlm@10 17
rlm@10 18 This library provides random number generators with a common
rlm@10 19 stream interface. They all produce pseudo-random numbers that are
rlm@10 20 uniformly distributed in the interval [0, 1), i.e. 0 is a
rlm@10 21 possible value but 1 isn't. For transformations to other
rlm@10 22 distributions, see clojure.contrib.probabilities.monte-carlo.
rlm@10 23
rlm@10 24 At the moment, the only generator provided is a rather simple
rlm@10 25 linear congruential generator."}
rlm@10 26 clojure.contrib.probabilities.random-numbers
rlm@10 27 (:refer-clojure :exclude (deftype))
rlm@10 28 (:use [clojure.contrib.types :only (deftype)])
rlm@10 29 (:use [clojure.contrib.stream-utils :only (defstream)])
rlm@10 30 (:use [clojure.contrib.def :only (defvar)]))
rlm@10 31
rlm@10 32 ;; Linear congruential generator
rlm@10 33 ;; http://en.wikipedia.org/wiki/Linear_congruential_generator
rlm@10 34
rlm@10 35 (deftype ::lcg lcg
rlm@10 36 "Create a linear congruential generator"
rlm@10 37 {:arglists '([modulus multiplier increment seed])}
rlm@10 38 (fn [modulus multiplier increment seed]
rlm@10 39 {:m modulus :a multiplier :c increment :seed seed})
rlm@10 40 (fn [s] (map s (list :m :a :c :seed))))
rlm@10 41
rlm@10 42 (defstream ::lcg
rlm@10 43 [lcg-state]
rlm@10 44 (let [{m :m a :a c :c seed :seed} lcg-state
rlm@10 45 value (/ (float seed) (float m))
rlm@10 46 new-seed (rem (+ c (* a seed)) m)]
rlm@10 47 [value (assoc lcg-state :seed new-seed)]))
rlm@10 48
rlm@10 49 ;; A generator based on Clojure's built-in rand function
rlm@10 50 ;; (and thus random from java.lang.Math)
rlm@10 51 ;; Note that this generator uses an internal mutable state.
rlm@10 52 ;;
rlm@10 53 ;; The state is *not* stored in the stream object and can thus
rlm@10 54 ;; *not* be restored!
rlm@10 55
rlm@10 56 (defvar rand-stream (with-meta 'rand {:type ::rand-stream})
rlm@10 57 "A random number stream based on clojure.core/rand. Note that this
rlm@10 58 generator uses an internal mutable state. The state is thus not stored
rlm@10 59 in the stream object and cannot be restored.")
rlm@10 60
rlm@10 61 (defstream ::rand-stream
rlm@10 62 [dummy-state]
rlm@10 63 [(rand) dummy-state])