Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
What's new
4
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Switch to GitLab Next
Sign in / Register
Toggle navigation
Menu
Open sidebar
Kisaragi Hiu
dotfiles
Commits
5e39e6c9
Commit
5e39e6c9
authored
Aug 08, 2018
by
Kisaragi Hiu
Browse files
rewrite random-string in picolisp
...so I can use it in Termux.
parent
2c4ed46f
Changes
1
Hide whitespace changes
Inline
Side-by-side
random-string
View file @
5e39e6c9
#
!/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*
))
)
(
random-string
)))
(
prinl (random-string len))
(bye
)
# vim: filetype=picolisp
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment