# 数学 趣味数学：关于概率的蒙提霍尔悖论 (也称为三门问题)

chenge · 2017年09月27日 · 最后由 Angus 回复于 2018年01月15日 · 9329 次阅读

## 代码

``````#!/usr/bin/ruby -s
# -*- coding: euc-jp -*-
# -*- Ruby -*-

def usage
name = File::basename \$0
print <<EOU
#{name}: ¥â¥ó¥Æ¥£¥Û¡¼¥ëÌäÂê¤ò¥·¥ß¥å¥ì¡¼¥È
(Îã)
#{name} 10  ¢ª 10 ²ó»î¹Ô
(¥ª¥×¥·¥ç¥óÎã)
-c         ¤Þ¤¿¤Ï -change   ¢ª ¥ä¥®¤ò¸«¤ÆÈâ¤òÊÑ¤¨¤ë
-s=777     ¤Þ¤¿¤Ï -seed=777 ¢ª Íð¿ô¤Î¼ï¤ò»ØÄê
-help      ¤Þ¤¿¤Ï -h        ¢ª ¤³¤Î¥á¥Ã¥»¡¼¥¸¤òÉ½¼¨
EOU
end

#####################################

if (\$help || \$h || ARGV.empty?)
usage
exit 0
end

\$change ||= \$c

\$seed ||= \$s
srand(\$seed.to_i) if \$seed

\$n = ARGV.shift.to_i

def choice(a)
a[rand(a.length)]
end

#####################################

\$doors = 'ABC'.split //
\$n.times{
car = choice \$doors
you = choice \$doors
goat = choice(\$doors - [car, you])
you = choice(\$doors - [you, goat]) if \$change
result = (car == you) ? 'O' : 'X'
puts result
}

``````

``````\$ make long
(no change)
./monty.rb 10000 | ../count.rb
O: 3327 (33.27%)
X: 6673 (66.73%)
(change)
./monty.rb -c 10000 | ../count.rb
O: 6609 (66.09%)
X: 3391 (33.91%)

``````

qichunren 回复

:e++enc=euc-jp