Run Code
|
API
|
Code Wall
|
Users
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Blog
Monty Hall Problem Simulated
Language:
Ada
Assembly
Bash
C#
C++ (gcc)
C++ (clang)
C++ (vc++)
C (gcc)
C (clang)
C (vc)
Client Side
Clojure
Common Lisp
D
Elixir
Erlang
F#
Fortran
Go
Haskell
Java
Javascript
Kotlin
Lua
MySql
Node.js
Ocaml
Octave
Objective-C
Oracle
Pascal
Perl
Php
PostgreSQL
Prolog
Python
Python 3
R
Rust
Ruby
Scala
Scheme
Sql Server
Swift
Tcl
Visual Basic
Layout:
Vertical
Horizontal
-- Change these to see different results! local doors = 3 local max_tries = 9999 local switch_door = true -- Don't change -- local answers_correct = 0 local u = 0 local function random(x, y) u = u + 1 if x ~= nil and y ~= nil then return math.floor(x +(math.random(math.randomseed(os.time()+u))*999999 %y)) else return math.floor((math.random(math.randomseed(os.time()+u))*1000)) end end for i = 1, max_tries do local correct_door = random(1, doors) local door_chosen = random(1, doors) local door_revealed = random(1, doors) if door_revealed == door_chosen or door_revealed == correct_door then door_revealed = (door_revealed + 1)%doors + 1 end if switch_door then repeat door_chosen = (door_chosen + 1)%doors +1 until door_revealed ~= door_chosen end if correct_door == door_chosen then answers_correct = answers_correct + 1 end end print("Percentage of correct answers: %" .. math.floor( (answers_correct/max_tries)*1000)/10 ) print("\nCorrect answers: " .. answers_correct) print("Incorrect answers: " .. max_tries - answers_correct)
[
+
]
Show input
Absolute running time: 0.26 sec, cpu time: 0.1 sec, memory peak: 5 Mb, absolute service time: 0,45 sec
edit mode
|
history
|
discussion
Percentage of correct answers: %44.2 Correct answers: 4429 Incorrect answers: 5570