Lisp: Generating a list of 10 random integers
common-lisp, lisp
Solution
Write a function that returns a new list with 10 random integers:
(defun randlist ()
(let ((lst ()))
(dotimes (i 10)
(setf lst (cons (random 101) lst)))
lst))
And then you can assign its result to a variable:
(defvar var1 (randlist))
DEMO.
Problem
this is part of a my first homework in common lisp. We have to define a variable representing any 10 random integers from 0 to 100: I am not sure what is being asked here. Should I write: `(setf var1 '())` Then, we have to define a function that generates a list of 10 random integers and returns a list that contains those numbers. Here is what I wrote, but I keep getting NIL as my output. Do you know what's wrong with the code: ``` (setf *random-state* (make-random-state t)) (setf var1 '()) (defun randlist (var1) (dotimes (i 10) (setf temp (random 101)) (cons 'temp var1 ) ) ) ```