HomeAboutCodePastes
#!/usr/bin/csi -s
(import chicken.random
        chicken.io
        srfi-13
        srfi-18
        miscmacros
        chicken.fixnum)

(define char-len 32)

(define (bytes)
  (substring
   (string-filter
    (cut (disjoin char-numeric? char-alphabetic?) <>)
    (random-bytes (make-string 500)))
   0 char-len))

(define (bytes-single)
  (define buf (make-string 1))
  (list->string
   (let loop ((byte (random-bytes buf))
              (i 0)
              (lst '()))
     (let ((chr (string-ref byte 0)))
       (cond ((= i char-len)
              lst)
             ((or (char-numeric? chr)
                  (char-alphabetic? chr))
              (loop (random-bytes buf)
                    (+ i 1)
                    (cons chr lst)))
             (else
              (loop (random-bytes buf)
                    i
                    lst)))))))

(define (urandom)
  (call-with-input-file "/dev/urandom"
    (lambda (in)
      (list->string
       (let loop ((byte (read-byte in))
                  (i 0)
                  (lst '()))
         (let ((chr (integer->char byte)))
           (cond ((= i char-len)
                  lst)
                 ((or (char-numeric? chr)
                      (char-alphabetic? chr))
                  (loop (read-byte in)
                        (+ i 1)
                        (cons chr lst)))
                 (else
                  (loop (read-byte in)
                        i
                        lst)))))))))

;;; Times compiled at -O3
;; 50-90
(time (dotimes (i 1000000)
        (bytes)))
;; 75-90
(time (dotimes (i 1000000)
        (bytes-single)))
;; 40-45
(time (dotimes (i 1000000)
        (urandom)))

plain