Commit 5e39e6c9 authored by Kisaragi Hiu's avatar Kisaragi Hiu
Browse files

rewrite random-string in picolisp I can use it in Termux.
parent 2c4ed46f
#!/usr/bin/env clisp
; vim: filetype=lisp
; random-string [length]
#!/usr/bin/pil -argv dummy len
# ^ bind first arg to `dummy`, second to `len`
# if length is a file, it would be loaded
# a bit of a hack around Picolisp's loading mechanism
# random-string [length]
(defvar *charset* "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
# (time) is only down to seconds, can't just seed the PRNG on every call to select-random-item.
(seed (+ (date) (time)))
(setq *charset* (chop "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"))
(defun select-random-item (seq)
(elt seq (random (length seq)
;; create a newly randomly seeded random state
(make-random-state t))))
(de select-random-item (seq)
(car (nth seq
(rand 1 (length seq)))))
(defun random-string (&optional (len 16))
(map 'string
(lambda (x) (select-random-item *charset*))
(make-string len)))
(de random-string (len)
(if (not len) (setq len 16))
(if (str? len) (setq len (format len)))
(mapcar '(() (select-random-item *charset*))
(range 1 len)))
(format t (if *args*
(random-string (parse-integer (first *args*)))
(prinl (random-string len))
# vim: filetype=picolisp
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment