Reputation: 121
I'm experimenting with Genetic Algorithms using Lisp, I want to generate a list of lists that contains five random numbers.
I can generate that list but all the sub-lists are composed of the same random numbers and that is because I'm not sure I'm managing the "random state" properly.
Can anyone give me an hint?
here is the code:
(setf *random-state* (make-random-state t))
(defun random_in
(min max)
(+ (random (+ (- max min) 1) *random-state*) min))
(defun create_chromosome
(min max)
(list (random_in min max) (random_in min max) (random_in min max) (random_in min max) (random_in min max)))
(defun create_population
(individuals min max)
(make-list individuals :initial-element (create_chromosome min max)))
(write (create_population 3 10 100))
The output of this program is:
((54 51 85 61 44) (54 51 85 61 44) (54 51 85 61 44))
but I want each list composed by different random numbers.
Thank you for your time.
Upvotes: 1
Views: 924
Reputation: 51521
When you use :initial-element
with make-list
, that element is created only once.
One way to achieve what you want:
(loop :repeat individuals
:collect (create-chromosome min max))
Upvotes: 6