Download or view redballs.frink in plain text format
/** Probabilistic solver for a logic puzzle:
https://www.quantamagazine.org/perplexing-the-web-one-probability-puzzle-at-a-time-20240829/
"You have an urn filled with 100 balls, some red and some green. You can’t
see inside; all you know is that someone determined the number of red balls
by picking a number between zero and 100 from a hat. You reach into the urn
and pull out a ball. It’s red. If you now pull out a second ball, is it
more likely to be red or green (or are the two colors equally likely)?"
*/
trials = million
redFirstTrials = 0 // Number of trials in which the first ball drawn is red
redSecond = 0 // Number of trials in which the second ball drawn is red
for trial = 1 to trials
{
numRed = random[0, 100]
// Create an array of 100 balls which are either "red" or "green" with the
// number of red given by numRed
urn = new array[[100], {|x, data| x < data ? "red" : "green"}, numRed]
urn.shuffle[] // To eliminate any bias, shuffle using Fisher-Yates-Knuth
firstBall = urn.removeRandom[] // Draw a random first ball, removing it
if firstBall == "red"
{
// First ball was red
redFirstTrials = redFirstTrials + 1
secondBall = urn.removeRandom[] // Draw a random second ball
if secondBall == "red"
redSecond = redSecond + 1
}
}
// Calculate the ratio of second balls that are also red
println["The probability that the second ball is also red:"]
println[redSecond / redFirstTrials]
Download or view redballs.frink in plain text format
This is a program written in the programming language Frink.
For more information, view the Frink
Documentation or see More Sample Frink Programs.
Alan Eliasen was born 20218 days, 0 hours, 17 minutes ago.