[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Chicken-users] srfi-27 - producing pseudo and truly random numbers

From: Kon Lovett
Subject: Re: [Chicken-users] srfi-27 - producing pseudo and truly random numbers over a uniform distribution
Date: Tue, 6 Nov 2007 14:03:02 -0800

On Oct 28, 2007, at 2:55 PM, Terrence Brannon wrote:

In addition to the bug I filed on this egg - ticket/346

I have a question about common usage of this module.

I would like to produce one function, roll-fixed, which produces
pseudorandom numbers in the documented fashion of random-real and
random in SRFI-27. Such that the sequence of randoms (over a uniform
distribution) is predictable on each new startup of the intepreter:

(define *fixed-seed* (expt 7 5))

(define roll-fixed
  (let ([side-effect (set-seed *fixed-seed*)])
     (lambda (n)
        (if (zero? n)
            (random-integer-based-on-fixed-seed n)))))

(use entropy-fixed)

; A fixed seed entropy struct

(define fixed-seed
   (let ([*fixed-seed* (expt 7 5)]) (lambda () *fixed-seed*))
   "fixed seed: (expt 7 5)" ) )

; This uses the 'current-random-source-structure'

(define (roll-fixed n)
  (if (zero? n)
      (random-real) ) )

; As default entropy:
(current-entropy-source-structure fixed-seed)
(random-source-randomize! default-random-source)

; Or:
(random-source-randomize!/entropy default-random-source fixed-seed)

Also, the random-state of a random-structure can be saved & restored. So each run can load a file w/ a random-state before getting a random sequence.

Note, until the new release is posted 'entropy-fixed' & 'random- source-randomize!/entropy' are unavailable. 'entropy-fixed' is in the posted egg but is not in the .setup (I forgot it).

Additionally, I would like a function, roll-truly-random, which again
can produce reals or integer randoms, but whose sequence of randoms
(over a uniform distribution) is different across each new startup of
the interpreter.

(define roll-truly-random
  (let ([side-effect (set-seed (truly-random-seed))])
     (lambda (n)
        (if (zero? n)
            (random-integer-based-on-truly-random-seed n)))))

What is '(truly-random-seed)'?

Currently the entropy structures 'dev-random-entropy', 'dev-urandom- entropy' are defined for unix & 'crypt-random-entropy' for windows, and 'current-seconds-entropy' (for both platforms).

Note that SRFI-27 does not define entropy sources - it just assumes there is one.

I'm sorry to beg for such hand-holding, but it seems like the Issues
section of the docs does admit that this library could use a few good
examples. It also mentions that the API creates something of a hurdle
for my desire to have two distinct random structures available in two
distinct functions.

You cannot have distinct random structures but you can have distinct random sources. (Well, you can, MWC is distinct from MOA but I doubt you want to define your own random number algorithm.)

(use mwc mrg32k3a moa)

(define my-mwc-source ((MWC 'make-random-source)))
(define my-moa-source ((MOA 'make-random-source)))
(define my-mrg-source ((MRG32k3a 'make-random-source)))

(define my-mwc-random-integer (random-source-make-integers my-mwc- source))
(define my-mwc-random-real (random-source-make-reals my-mwc-source))
(random-source-randomize!/entropy my-mwc-source fixed-seed)
(my-mwc-random-integer 23) ; [0 23)

(use srfi-27-distributions)

(define my-mwc-random-integers (make-uniform-random-integers 22 15 1 my-mwc-source))
(my-mwc-random-integers) ; [15 22] by 1

SRFI-27 was written assuming only one pseudo-random algorithm & a simple module system. While I could have extracted the random source agnostic procedures it seemed better to just follow the spec. In any case a random structure is open upon load so these procedures are always available.

Chicken-users mailing list

Best Wishes,

reply via email to

[Prev in Thread] Current Thread [Next in Thread]