好的,最后一个问题,我将在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.如何让(random)
随机行动?
答案 0 :(得分:25)
你需要在程序开始时播种随机状态。
(setf *random-state* (make-random-state t))
;; # this initializes the global random state by
;; "some means" (e.g. current time.)
答案 1 :(得分:0)
我认为如果你定义一个带有随机数的函数,当你调用函数时它不会被调用,实际上,它会在你加载文件时确定,当它运行这个定义时,它被修复为那个价值。然后你每次都调用这个函数,数字总是一样的。当我每次调用一个随机变量传递给函数时,每次都是随机的。至少,我在我的程序中经历的那些