2011/02/07

ランダムな文字列

(use math.mt-random)
(use srfi-14)
(use srfi-1 :only (iota))

(define rand
  (let1 m (make <mersenne-twister>)
    (^ (:optional (len 2))
       (mt-random-integer m len))))

(define (random-char src)
  (string-ref src (rand (string-length src))))

(define (random-string src len)
  (let rec ((len len)(acc '()))
    (if (zero? len)
        (list->string acc)
        (rec (- len 1)(cons (random-char src) acc)))))

(define (sequential-src :optional (len 255))
  (list->string (map integer->char (iota len))))



;; --- test

(dotimes (i 10)
  (print (random-string (char-set->string #[a-zA-Z0-9])
                        (+ 10 (rand 6)))))
;; Bp5xazgBweRl
;; NFihHScYVLrGj
;; KDBNedcogJB
;; YVY5VhDX4c
;; rcSM5CdB8UbUth
;; PDNbRy7l30e3Sn
;; Yn79nl6WyhSn
;; M4ugWYCllpq
;; jItJYe1G5PoM
;; pHCOxjYPra
;; #t


(use rfc.md5)
(use util.digest)
(dotimes (i 10)
  (print (digest-hexify (md5-digest-string (random-string (sequential-src 128) 10)))))
;; 2eccd115ab4cf544972215127d01efb7
;; c6542575cddcbc67a4a151ab042005ff
;; 31edccbadce32cdd3239e16999cfdbc9
;; 13fac266ff5713cc76f665c90db06dfe
;; 61b741f33bdaf81533f23b1837b85f59
;; ad5a55d2992279d8cd9104f8174c7fe1
;; 044bc4528cb92291d060c5b2c40ec4c3
;; 393b43b9cb3db1b758ba9cdc57a75f1e
;; 2c2de35251b753aef7db21b8c50911aa
;; 44fb0ae9f60707329d9ed9c811f8bbad
;; #t


Scheme手習い

0 件のコメント:

コメントを投稿