Random (随机)在公共Lisp中不是那么随机吗?

Random (随机)在公共Lisp中不是那么随机吗?,random,lisp,common-lisp,sbcl,Random,Lisp,Common Lisp,Sbcl,好的,最后一个问题,我将用Common Lisp完成我的数字猜测游戏D每当游戏开始时(或第一场游戏后新游戏开始),调用以下函数 ;;; Play the game (defun play () ;; If it's their first time playing this session, ;; make sure to greet the user. (unless (> *number-of-guesses* 0) (welcome-user)

好的,最后一个问题,我将用Common Lisp完成我的数字猜测游戏D每当游戏开始时(或第一场游戏后新游戏开始),调用以下函数

;;; Play the game
(defun play ()
    ;; If it's their first time playing this session,
    ;; make sure to greet the user.
    (unless (> *number-of-guesses* 0)
        (welcome-user))
    ;; Reset their remaining guesses
    (setq *number-of-guesses* 0)
    ;; Set the target value
    (setq *target*
        ;; Random can return float values,
        ;; so we must round the result to get
        ;; an integer value.
        (round
            ;; Add one to the result, because
            ;; (random 100) yields a number between
            ;; 0 and 99, whereas we want a number
            ;; from 1 to 100 inclusive.
            (+ (random 100) 1)))
    (if (eql (prompt-for-guess) t)
        (play)
        (quit)))

因此,假设玩家每次开始游戏时,
*target*
都应设置为1-100之间的新随机整数。但是,每次,
*target*
都默认为82。如何使
(随机)
行为。。。随机?

您需要在程序开始时对随机状态进行种子设定

(setf *random-state* (make-random-state t))
;; # this initializes the global random state by
;;   "some means" (e.g. current time.)

我认为如果你定义了一个包含随机数的函数,那么当你调用这个函数时,它不会被调用,实际上,它会在你加载到文件中时被确定,当它运行这个定义时,它会被固定到那个值。然后每次调用该函数时,号码将始终相同。当我每次调用一个带有随机变量的函数时,它每次都是随机的。至少,我在我的课程中所经历的不一定是正确的。CL规范没有强制使用当前时间,它只是说“通过某种方式随机初始化”。这是错误的<代码>随机是一种功能,与其他许多功能一样依赖于副作用。它在与任何其他函数调用相同的情况下进行计算。如果在宏中调用它(而不是返回与调用它对应的表单),那么它将返回单个数字,而不是某个计算结果为随机数的特殊对象(请注意,这种特殊对象将是类似于
(random 1.0)
)的表单)。也许这就是你的意思。