Advertisement
Guest User

Untitled

a guest
Dec 27th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.54 KB | None | 0 0
  1. math.randomseed(os.time()) -- ignore this
  2. local TESTS = 1000000 -- how many tests are done
  3. local SPECKS_PER_BOX = 10000 -- how many specks of dust get put in each box
  4. local boxes = {
  5.     [1] = {}, -- gold dust
  6.     [2] = {}, -- silver with gold speck
  7.     [3] = {}  -- silver dust
  8. }
  9.  
  10. -- Put the specks of gold into the box
  11. for i = 1, SPECKS_PER_BOX do -- do this SPECKS_PER_BOX times
  12.     table.insert(boxes[1], "g") -- put a speck of gold dust into the gold dust box
  13.     table.insert(boxes[2], "s") -- put a speck of silver dust into the silver dust with gold speck box
  14.     table.insert(boxes[3], "s") -- put a speck of silver dust into the silver dust box
  15. end
  16. -- each box now has SPECKS_PER_BOX specks of corresponding dust in it
  17.  
  18. -- now, at random, we replace one of the silver specks in the "silver dust with gold speck" box with a gold speck
  19. boxes[2][math.random(1,SPECKS_PER_BOX)] = "g"
  20.  
  21. -- now, we simulate the question
  22. local restOfBoxWasGold = 0
  23. local goldSpecksChosen = 0
  24. for i = 1, TESTS do -- do everything in this block TESTS times
  25.     local chosenBox = boxes[math.random(1, 3)] -- You pick a box at random,
  26.     local chosenSpeck = chosenBox[math.random(1, #chosenBox)] -- then pick a random speck of dust from that box.
  27.     if chosenSpeck == "g" then -- Given that you chose a gold speck,
  28.         -- what is the probability that the rest of the box is gold?
  29.         if chosenBox == boxes[1] then
  30.             restOfBoxWasGold = restOfBoxWasGold + 1
  31.         end
  32.         goldSpecksChosen = goldSpecksChosen + 1
  33.     end
  34. end
  35.  
  36. print("Probability: " .. restOfBoxWasGold/goldSpecksChosen * 100 .. "%")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement