balls = ...
line. As usual, N
will be the number of times you'd like to reset everything, and pick a ball. The varible gotit
("got it") will count the number of times you picked the ball whose color you wanted.
N
and edit the ??
to be a color ball you'd like to pick ("r", "w", or "b").
balls = {"r","r","r","r","r","r","w","w","w","w","b","b","b","b","b"}
N=10
gotit = 0
for i=1,N do
b = math.random(1,#balls)
print("picked ",balls[b]," ball")
if balls[b] ~= ?? then
gotit = gotit + 1
end
end
print(gotit/N)
Here's another way of thinking about it: The sample space of balls is 15 total possible. If you have an equally likely chance of choosing any ball, then there's a 1/15 chance of choosing any ball. Since there are 6 red balls, you then have a 6×1/15 chance of choosing a red ball. Picking a white or blue ball can be calculated using a similar argument. (Chance of blue=5/15=1/3, chance of white=4/15.)
What about the "not red?" Well, this will just be 1-"the chance of picking red." or 1−0.4=0.6. "1-something" in this business kind of means "not," or "not red." Why? Well 1 means 100% certainty, and if 0.4 of the certainty goes into picking a red ball, then the "rest" or 1−0.4 would mean picking a ball that was not red.
You can also think of the "not red" in this way: ways of choosing a blue or white balltotal ways of choosing a ball which for "not red" (i.e. a blue or white ball) would be (4+5)/15=9/15=0.6.
Choosing a red or white ball can be computed in your code with if balls[b] == "r" or balls[b] == "w" then
. The answer can be computed via:
ways of choosing a red or white balltotal ways of choosing a ball
which here would be 10/15=0.3.