Given a mapping between items and their required probability of occurrence, generate a million items randomly subject to the given probabilities and compare the target probability of occurrence versus the generated values.

The total of all the probabilities should equal one. (Because floating point arithmetic is involved, this is subject to rounding errors).

aleph   1/5.0
beth    1/6.0
gimel   1/7.0
daleth  1/8.0
he      1/9.0
waw     1/10.0
zayin   1/11.0
heth    1759/27720 # adjusted so that probabilities add to 1
Related task

 Random number generator (device)


with javascript_semantics
constant lim = 1000000,
{names, probs} = columnize({{"aleph",   1/5},
                            {"beth",    1/6},
                            {"gimel",   1/7},
                            {"daleth",  1/8},
                            {"he",      1/9},
                            {"waw",     1/10},
                            {"zayin",   1/11},
                            {"heth",    1759/27720}})
sequence results = repeat(0,length(names))
for j=1 to lim do
    atom r = rnd()
    for i=1 to length(probs) do
        r -= probs[i]
        if r<=0 then
            results[i]+=1
            exit
        end if
    end for
end for
 
printf(1,"  Name   Actual Expected\n")
for i=1 to length(probs) do
    printf(1,"%6s %8.6f %8.6f\n",{names[i],results[i]/lim,probs[i]})
end for
