I quickly created this function in Lisp that resembles
numpy.random.randint(low, high=None, size=None, dtype='I')
The goal is simply to have a function that creates a list of random integers between low
and high
and that the list has a certain shape
. I did not add the ability to choose the dtype
as well, but this would be a possibility.
My function for doing this is the following:
(defun randint (&key (start 0) end (shape '(1)))
(if (> (length shape) 3)
(error "Shape can be maximally 3 long")
(loop for i from 1 upto (first shape)
if (not (null (rest shape)))
collect (loop for j from 1 upto (first (rest shape))
if (not (null (rest (rest shape))))
collect (loop for k from 1 upto (first (rest (rest shape)))
collect (+ start (random (+ 1 (- end start)))))
else
collect (+ start (random (+ 1 (- end start)))))
else
collect (+ start (random (+ 1 (- end start)))))))
Is there a more concise or more elegant way of writing this in Common Lisp?