Common Lisp 中的(随机)不那么随机?
好的,最后一个问题,我将用 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)
随机行动?
Okay, final question and I'll have finished my number guessing game in Common Lisp! :D Whenever the game starts (or a new game begins after the first game), the following function is called.
;;; 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)))
So supposedly, each time the player starts a game, *target*
should be set to a new random integer between 1-100. However, each time, *target*
defaults to 82. How do I make (random)
act... randomly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为,如果您定义一个带有随机数的函数,那么当您调用该函数时不会调用它,实际上,它会在您加载文件时确定,并且当它运行此定义时,它会固定为该值。然后你每次调用该函数时,数字将始终相同。当我每次调用函数时都将变量传递给随机函数时,每次都是随机的。至少,我在程序中经历过
I think that if you define a function with a random number in it, it is not called when you call the function, actually, it will be determined when you load in the file and when it runs this definition it is fixed to that value. Then you are calling the function each time, the number will always be the same. When I passed in a variable to the function with a random each time it was called, then it was random each time. At least, that what I experienced in my program
在 Gimp Scheme(Lisp 衍生品)中,我发现我需要使用以下内容:
(设置!*seed* (car (gettimeofday)))
这是来自时钟的随机数种子(种子是全局的)
您可能还需要:
(随机下一个)
这会使用种子生成下一个随机数
非真正随机的方法是设置种子:
(srand 12345)
;用于结果可重复的情况 - 在图形设计环境中很有用。
In Gimp Scheme (Lisp derivative) I found that I needed to use the following:
(set! *seed* (car (gettimeofday)))
This is the Random Number Seed From Clock (seed is global)
You may also need:
(random-next)
This generates the next Random Number Using Seed
The non-truly random method is to set the seed:
(srand 12345)
; used in cases where the result is to be repeatable - useful in graphic design contexts.
您需要在程序开始时播种随机状态。
You need to seed the random state at the start of the program.