# [metapost] Drawing a urn

Mojca Miklavec mojca.miklavec.lists at gmail.com
Thu Nov 29 20:37:06 CET 2007

```On Nov 29, 2007 1:09 PM, Martin Kaffanke wrote:
> Hi there!
>
> I'd like to draw an urn where we have a probability of .57 to get a dark
> ball when we choose a random ball.
>
> I have the following:
>
>  u := 5mm;
>  s := .05u;
>
>  draw (0,10u+s)--(0,.5u)..(.15u,.15u)..(.5u,0)
>       --(9.5u+2s,0)..(9.85u+2s,.15u)..(10u+2s,.5u)--(10u+2s , 10u+s);
>
>  path c;
>  for i=1 upto 10:
>    for j=1 upto 10:
>      c := fullcircle scaled 1u shifted (j*u-u/2+s,i*u-u/2+s);
>      if (i <= 5 ):
>         fill c withcolor .7white;
>      else:
>         fill c withcolor .4white;
>      fi;
>    endfor;
>  endfor;
>
> This looks nice, but it shows a fifty-fifty chance.  How would you go
> further to show a chance of .57 instead of .5?
>
> Second: !important! I'd like to have the balls randomized.  In a java
> application I do the same by creating 100 boolean values (true = dark,
> false = white) something like
>
> for i=1 upto 100:
>   if i <= .57:
>     values.append(.4white);
>   else:
>     values.append(.7white);
>   fi;
> endfor;
>
> mix(values);

Approach nr. 1: define 57 blacks (and write your own mixing function)
Approach nr. 2: assign colors randomly and fix afterwards if needed
until you have the desired number of them

Mojca

beginfig(1);
u := 5mm;
s := .05u;

draw (0,10u+s)--(0,.5u)..(.15u,.15u)..(.5u,0)
--(9.5u+2s,0)..(9.85u+2s,.15u)..(10u+2s,.5u)--(10u+2s , 10u+s);

boolean is_black[][];
numeric number_of_black; number_of_black := 0;
numeric probability_for_black; probability_for_black := 0.57;

path c;

for i=1 upto 10:
for j=1 upto 10:
if uniformdeviate(1) < probability_for_black:
is_black[i][j] := true;
number_of_black := number_of_black + 1;
else:
is_black[i][j] := false;
fi;
endfor;
endfor;

forever:
exitif number_of_black=57;

a := ceiling(uniformdeviate(9.9999)+0.0001); % prevent getting 0 (eps
should be used)
b := ceiling(uniformdeviate(9.9999)+0.0001);

if     (number_of_black<probability_for_black*100) and not is_black[a][b]:
is_black[a][b] := true;
number_of_black := number_of_black + 1;
elseif (number_of_black>probability_for_black*100) and is_black[a][b]:
is_black[a][b] := false;
number_of_black := number_of_black - 1;
fi;
endfor;

for i=1 upto 10:
for j=1 upto 10:
c := fullcircle scaled 1u shifted (j*u-u/2+s,i*u-u/2+s);
fill c withcolor
if is_black[i][j]: .3white
else: .7white
fi;
endfor;
endfor;
endfig;
end.
```