random-string.rkt 613 Bytes
Newer Older
Kisaragi Hiu's avatar
Kisaragi Hiu committed
1 2 3 4 5 6 7 8
#!/usr/bin/env racket
#lang racket
; vim: filetype=racket
; random-string [length]

(define charset (or (getenv "CHARSET")
                    "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"))

9 10
(define (select-random-item seq)
  (sequence-ref seq (random (sequence-length seq))))
Kisaragi Hiu's avatar
Kisaragi Hiu committed
11 12 13

(define (random-string [len 16])
  (list->string
14 15
    (map (λ (x) (select-random-item charset))
         (make-list len #f))))
Kisaragi Hiu's avatar
Kisaragi Hiu committed
16 17 18 19

(if (empty? (vector->list (current-command-line-arguments)))
  (displayln (random-string))
  (displayln (random-string (string->number (vector-ref (current-command-line-arguments) 0)))))