Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- math.randomseed(os.time()) -- ignore this
- local TESTS = 1000000 -- how many tests are done
- local SPECKS_PER_BOX = 10000 -- how many specks of dust get put in each box
- local boxes = {
- [1] = {}, -- gold dust
- [2] = {}, -- silver with gold speck
- [3] = {} -- silver dust
- }
- -- Put the specks of gold into the box
- for i = 1, SPECKS_PER_BOX do -- do this SPECKS_PER_BOX times
- table.insert(boxes[1], "g") -- put a speck of gold dust into the gold dust box
- table.insert(boxes[2], "s") -- put a speck of silver dust into the silver dust with gold speck box
- table.insert(boxes[3], "s") -- put a speck of silver dust into the silver dust box
- end
- -- each box now has SPECKS_PER_BOX specks of corresponding dust in it
- -- now, at random, we replace one of the silver specks in the "silver dust with gold speck" box with a gold speck
- boxes[2][math.random(1,SPECKS_PER_BOX)] = "g"
- -- now, we simulate the question
- local restOfBoxWasGold = 0
- local goldSpecksChosen = 0
- for i = 1, TESTS do -- do everything in this block TESTS times
- local chosenBox = boxes[math.random(1, 3)] -- You pick a box at random,
- local chosenSpeck = chosenBox[math.random(1, #chosenBox)] -- then pick a random speck of dust from that box.
- if chosenSpeck == "g" then -- Given that you chose a gold speck,
- -- what is the probability that the rest of the box is gold?
- if chosenBox == boxes[1] then
- restOfBoxWasGold = restOfBoxWasGold + 1
- end
- goldSpecksChosen = goldSpecksChosen + 1
- end
- end
- print("Probability: " .. restOfBoxWasGold/goldSpecksChosen * 100 .. "%")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement