Not sure what you mean.. You might mean to do
Code: Select all
(let ((functions (list (lambda () (random 5)))))) ;Or (alexandria:curry #'random 5)
(dotimes (i 10) ;But this will autoput differently each time.
(print (funcall (nth 0 functions)))))
Or you could be meaning that you want some sort of memoization, this is likely going to be slower than just storing random once:
Code: Select all
(defun memoized-random (upto)
(let ((mem -1))
(lambda () (if (< mem 0) (setq mem (random upto)) mem))))
It might be better just to make an array and store random values there when they arrive. Or do memoization
automatically, but that afaik uses an hash-table.(much slower than that array, which doesn't take much space if the numbers are dense..)
Note that if you want to in effect 'store' a sequence of random states, you could also just store the seed those came from(something like):
Code: Select all
(let (random-state)
(defun with-one-consistent-random (do) ;This only gives you one though.
(let ((*random-state* (or random-state (setq random-state *random-state*))))
(funcall do))));Or maybe
(defmacro with-consistent-random ((store-random) &body body)
"Stores the and uses the first *random-state* it encounters.
Be warned, Store-random could be called twice (once the setf-version), avoid having side-effects in it/it being slow."
`(let ((*random-state* (or ,store-random (setf ,store-random *random-state*)))
,@body))
Useful if you're making random trees or something, you'd only have to store the random seed to generate them. (Although the random generator changing/being implementation dependent might be an issue.)