[metapost] Drawing a urn

Hartmut Henkel hartmut_henkel at gmx.de
Thu Nov 29 14:28:29 CET 2007


On Thu, 29 Nov 2007, Martin Kaffanke wrote:

> 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.
>
> 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

they already already look rather mixed when you use an accumulator loop
(variable q), where you also easily can set the probability value, e.
g.:

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);

 q = 0;
 path c;
 for i=1 upto 10:
   for j=1 upto 10:
     %q := q + 0.5;
     %q := q + 0.5 + uniformdeviate(0.2) - 0.1;
     %q := q + 0.57;
     q := q + 0.57 + uniformdeviate(0.2) - 0.1;
     c := fullcircle scaled 1u shifted (j*u-u/2+s,i*u-u/2+s);
     if (q >= 1):
        fill c withcolor .7white;
        q := q - 1;
     else:
        fill c withcolor .4white;
     fi;
   endfor;
 endfor;


endfig;
end

it works without the uniformdeviate() as well, but this additional
randomization makes it look better particularly for cases like 0.5.

Regards, Hartmut


More information about the metapost mailing list