diff 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
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/clojure/contrib/probabilities/random_numbers.clj	Sat Aug 21 06:25:44 2010 -0400
     1.3 @@ -0,0 +1,63 @@
     1.4 +;; Random number generators
     1.5 +
     1.6 +;; by Konrad Hinsen
     1.7 +;; last updated May 3, 2009
     1.8 +
     1.9 +;; Copyright (c) Konrad Hinsen, 2009. All rights reserved.  The use
    1.10 +;; and distribution terms for this software are covered by the Eclipse
    1.11 +;; Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
    1.12 +;; which can be found in the file epl-v10.html at the root of this
    1.13 +;; distribution.  By using this software in any fashion, you are
    1.14 +;; agreeing to be bound by the terms of this license.  You must not
    1.15 +;; remove this notice, or any other, from this software.
    1.16 +
    1.17 +(ns
    1.18 +  ^{:author "Konrad Hinsen"
    1.19 +     :doc "Random number streams
    1.20 +
    1.21 +           This library provides random number generators with a common
    1.22 +           stream interface. They all produce pseudo-random numbers that are
    1.23 +           uniformly distributed in the interval [0, 1), i.e. 0 is a
    1.24 +           possible value but 1 isn't. For transformations to other
    1.25 +           distributions, see clojure.contrib.probabilities.monte-carlo.
    1.26 +
    1.27 +           At the moment, the only generator provided is a rather simple
    1.28 +           linear congruential generator."}
    1.29 +  clojure.contrib.probabilities.random-numbers
    1.30 +  (:refer-clojure :exclude (deftype))
    1.31 +  (:use [clojure.contrib.types :only (deftype)])
    1.32 +  (:use [clojure.contrib.stream-utils :only (defstream)])
    1.33 +  (:use [clojure.contrib.def :only (defvar)]))
    1.34 +
    1.35 +;; Linear congruential generator
    1.36 +;; http://en.wikipedia.org/wiki/Linear_congruential_generator
    1.37 +
    1.38 +(deftype ::lcg lcg
    1.39 +  "Create a linear congruential generator"
    1.40 +  {:arglists '([modulus multiplier increment seed])}
    1.41 +  (fn [modulus multiplier increment seed]
    1.42 +    {:m modulus :a multiplier :c increment :seed seed})
    1.43 +  (fn [s] (map s (list :m :a :c :seed))))
    1.44 +
    1.45 +(defstream ::lcg
    1.46 +  [lcg-state]
    1.47 +  (let [{m :m a :a c :c seed :seed} lcg-state
    1.48 +	value (/ (float seed) (float m))
    1.49 +	new-seed (rem (+ c (* a seed)) m)]
    1.50 +    [value (assoc lcg-state :seed new-seed)]))
    1.51 +
    1.52 +;; A generator based on Clojure's built-in rand function
    1.53 +;; (and thus random from java.lang.Math)
    1.54 +;; Note that this generator uses an internal mutable state.
    1.55 +;;
    1.56 +;; The state is *not* stored in the stream object and can thus
    1.57 +;; *not* be restored!
    1.58 +
    1.59 +(defvar rand-stream (with-meta 'rand {:type ::rand-stream})
    1.60 +  "A random number stream based on clojure.core/rand. Note that this
    1.61 +   generator uses an internal mutable state. The state is thus not stored
    1.62 +   in the stream object and cannot be restored.")
    1.63 +
    1.64 +(defstream ::rand-stream
    1.65 +  [dummy-state]
    1.66 +  [(rand) dummy-state])