How to resolve the algorithm Probabilistic choice step by step in the PL/I programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Probabilistic choice step by step in the PL/I programming language

Table of Contents

Problem Statement

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

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Probabilistic choice step by step in the PL/I programming language

Source code in the pl/i programming language

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

  

You may also check:How to resolve the algorithm Exponentiation order step by step in the PL/I programming language
You may also check:How to resolve the algorithm Generate lower case ASCII alphabet step by step in the PL/I programming language
You may also check:How to resolve the algorithm Dot product step by step in the PL/I programming language
You may also check:How to resolve the algorithm Price fraction step by step in the PL/I programming language
You may also check:How to resolve the algorithm Keyboard input/Obtain a Y or N response step by step in the PL/I programming language