gpt4 book ai didi

lisp - 第一个更大的(常见的)lisp 程序 -> 'random' 没有按预期工作

转载 作者:太空宇宙 更新时间:2023-11-03 18:50:41 25 4
gpt4 key购买 nike

只是为了好玩,我写了一个 "monty hall problem" 的模拟在 Python 中。后来用Lua试验了一下,决定用Lua再写一遍,看看对比一下会怎么样。这是一次非常有趣的体验,尽管程序看起来非常相似(Lua 版本略短)。最近我开始尝试使用 CL 并想再次做同样的事情。但是,当我运行它时,它的行为并不像预期的那样。由于某种原因,反复无常的玩家(应该有 66% 的获胜机会)与天真玩家(有 50% 的获胜机会)的得分几乎相同。

有人可以给我提示哪里出了问题吗?这不是家庭作业或类似的东西,只是我第一次尝试用 CL 编写更大的程序。除了有关上述问题的提示外,我还欢迎有关如何改进我的风格的建议。我猜它仍然很像 Python(它或多或少是直接翻译)。

(defun choose-one (l)
"Ramdomly chooses one element of the given list"
(nth (random (length l)) l))

(defun remove-one (l)
"Randomly removes one element of the given list"
(remove (choose-one l) l))

(defun naive-player (initial-choice possible-choices)
"The naive player randomly picks one choice. Should have a 50% chance to win."
initial-choice ;keep compiler happy
(choose-one possible-choices))

(defun stubborn-player (initial-choice possible-choices)
"The stubborn player sticks with his initial choice. Should have a 33% chance to win."
possible-choices ;keep compiler happy
initial-choice)

(defun erratic-player (initial-choice possible-choices)
"The erratic player will always change his choice. Should have a 66% chance to win."
(choose-one (remove initial-choice possible-choices)))

(defun host-offer (prize possible-choices)
"The host reveals one wrong choice."
(let ((remaining (remove prize possible-choices)))
(remove (choose-one remaining) possible-choices)))

(defun one-game (playerfn choices)
"Simulates a single game with the given player. Evaluates to T if the player won."
(let ((prize (choose-one choices))
(player-choice (choose-one choices)))
(eq (funcall playerfn player-choice (host-offer prize choices)) prize)))


(defun multiple-games (num-games)
"Simulates the given number of games with all players. Evaluates to a result list."
(let ((choices '(door_a door_b door_c))
(naive-score 0)
(stubborn-score 0)
(erratic-score 0))
;(progn
(dotimes (i num-games)
;(progn
(if (one-game #'naive-player choices)
(incf naive-score))
(if (one-game #'stubborn-player choices)
(incf stubborn-score))
(if (one-game #'erratic-player choices)
(incf erratic-score)));)
(list
(list 'naive-player naive-score)
(list 'stubborn-player stubborn-score)
(list 'erratic-player erratic-score))));)

;; Run simulation and display results
(defparameter *num-games* 10000)
(format *standard-output* "--- Monty Hall ---~%")
(format *standard-output* "Simulating ~D games...~%" *num-games*)
(let ((result (multiple-games *num-games*)))
(format *standard-output* "~{~{~A score: ~D~}~%~}" result))

输出(例如):

--- Monty Hall ---
Simulating 10000 games...
NAIVE-PLAYER score: 5014
STUBBORN-PLAYER score: 3333
ERRATIC-PLAYER score: 4968

最佳答案

主机报价功能错误。在定义的 Monty Hall 问题中,主机永远不会打开玩家选择的门,但您的程序没有考虑到这一点。如果问题得到解决,程序将返回预期结果。

关于lisp - 第一个更大的(常见的)lisp 程序 -> 'random' 没有按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10725200/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com