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)


 probch: Proc Options(main);
 Dcl prob(8) Dec Float(15) Init((1/5.0),      /* aleph  */
                                (1/6.0),      /* beth   */
                                (1/7.0),      /* gimel  */
                                (1/8.0),      /* daleth */
                                (1/9.0),      /* he     */
                                (1/10.0),     /* waw    */
                                (1/11.0),     /* zayin  */
                                (1759/27720));/* heth   */
 Dcl what(8) Char(6) Init('aleph ','beth  ','gimel ','daleth',
                          'he    ','waw   ','zayin ','heth  ');
 Dcl ulim(0:8) Dec Float(15) Init((9)0);
 Dcl i Bin Fixed(31);
 Dcl ifloat Dec Float(15);
 Dcl one    Dec Float(15) Init(1);
 Dcl num    Dec Float(15) Init(1759);
 Dcl denom  Dec Float(15) Init(27720);
 Dcl x      Dec Float(15) Init(0);
 Dcl pr     Dec Float(15) Init(0);
 Dcl (n,nn) Bin Fixed(31);
 Dcl cnt(8) Bin Fixed(31) Init((8)0);
 nn=1000000;
 Do i=1 To 8;
   ifloat=i+4;
   If i<8 Then
     prob(i)=one/ifloat;
   Else
     prob(i)=num/denom;
   Ulim(i)=ulim(i-1)+prob(i);
   /* Put Skip list(i,prob(i),ulim(i));*/
   End;
 Do n=1 To nn;
   x=random();
   Do i=1 To 8;
     If x<ulim(i) Then Leave;
     End;
   cnt(i)+=1;
   End;
 Put Edit('letter    occurs    frequency  expected ')(Skip,a);
 Put Edit('------    ------   ---------- ----------')(Skip,a);
 Do i=1 To 8;
   pr=float(cnt(i))/float(nn);
   Put Edit(what(i),cnt(i),pr,prob(i))(Skip,a,f(10),x(2),2(f(11,8)));
   End;
 End;