During implementation and testing of different small hobby projects I often reach the point where I need (at least I like it more) some random data beyond the usual foo-bar strings or lists. Random here does not mean really mathematical random data but just something that looks random to humans.
Therefore I came up with the following:
;;;; random.lisp ;;; Time-stamp: <2019-05-27 14:53:38 m.buchmann> ;;; ;;; A short sketch of some random data generating functions. ;;; (ql:quickload "alexandria") ;; * Choosing a random element from a given list. (defun random-draw (list &aux (len (length list)) (pos (random len))) "Returns a random element from LIST." (nth pos list)) ;;; * Generating a random element (defun random-element (type &key (radix 10) (case :up)) "Returns a random element of type :digit (depending on :radix) or :character (case given by :case :up, :down or :both). Limited to 7bit ASCII characters from A to z." (let ((char-range (ecase case (:up (alexandria:iota 26 :start 65)) (:down (alexandria:iota 26 :start 97)) (:both (append (alexandria:iota 26 :start 65) (alexandria:iota 26 :start 97)))))) (ecase type (:digit (random radix)) (:character (code-char (random-draw char-range)))))) (defun random-list (len &key (type :digit) (radix 10) (case :up)) "Returns a list of length LEN filled with random elements of TYPE :digit or :character." (loop :for i from 0 below len :collect (random-element type :radix radix :case case))) (defun random-string (len &key (case :up) &aux (result (make-array len :element-type 'character))) "Returns a random string of length LEN and :case (:up, :down or :both)." (loop :for i :from 0 :below len :do (setf (aref result i) (random-element :character :case case))) result)
I did not pack it in a proper package etc. yet because my use case is pretty simple and usually temporary. I was wondering if other people have implemented similar things or if the need I felt was just so individual that no one else really cares about it. I did not find other libraries for this.
I think it could be easily extended to supply random-arrays, hash-tables and so on. Also the character encoding could be improved to deliver more than 7bit ASCII and this in a portable way.
Any comments on the usability, style etc. are gratefully acknowledged.