chicken-users
[Top][All Lists]
Advanced

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

[Chicken-users] Re: srfi-27.egg


From: Kon Lovett
Subject: [Chicken-users] Re: srfi-27.egg
Date: Fri, 27 Jul 2007 21:13:23 -0700


On Jul 27, 2007, at 1:17 PM, Ben Kurtz wrote:


Hello!

I was looking at using your srfi-27 egg for Chicken.

I'm interested in generating a random u8vector of a given length.

Looks like there is some way to do it, but the doc is confusing... So I figured I'd just ask :)

What's the shortest path to a random u8vector using srfi-27.egg?

Thanks much!

- Ben Kurtz

There is no direct way to do it w/ SRFI-27, i.e. no (fill-random- u8vector ?u8vec [?rand-genr-proc]). The following will return new u8vectors of the specified length with random contents from the current random source.

(use syntax-case srfi-4 srfi-27-distributions srfi-4-comprehensions)

(define u8rnd (make-uniform-random-integers 255))
(define (random-u8vector len #!optional (gen u8rnd))
  (u8vector-of-length-ec len (: x 0 len) (gen)) )

Or the following, which uses a random-source-structure:

(use syntax-case srfi-4 lexmod srfi-27)

(define (random-u8vector v-or-n . rest)
  (let ([src (optional rest (current-random-source-structure))]
        [vec (if (u8vector? v-or-n) v-or-n (make-u8vector v-or-n))])
    (let ([len (u8vector-length vec)])
      (let-imports ([src random-integer])
        (do ([i 0 (fx+ i 1)])
            [(fx= i len) vec]
          (u8vector-set! vec i (random-integer 255)) ) ) ) ) )

random-u8vector takes a new vector length or existing vector and an optional random-source-structure, like MWC, MRG32k3a or MOA.

#;3> (use mwc)
; loading /usr/local/lib/chicken/1x/mwc.so ...
; loading /usr/local/lib/chicken/1x/mwc-primitives.so ...
#;4> (random-u8vector 20 MWC)
#u8(1 105 17 140 219 56 22 130 169 240 85 89 56 154 116 91 110 128 50 233)

You can also fill a u8vector from an entropy source but probably not what you want.

BTW: There is also the "gsl-srfi-27" which uses the GNU Scientific Library but w/ slightly differing signatures.

HTH,
Kon





reply via email to

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