               JOB  1401 RANDOM
               CTL  6111
     *
     ***********************************************
     * 2-DIGIT NORMALLY DISTRIBUTED RANDOM NUMBERS *
     *                                             *
     * BY RONALD MAK                               *
     * NOVEMBER 29, 2019                           *
     ***********************************************
     *
     * INDEX REGISTERS
               ORG  87
     X1        DCW  @004@  POSITION IN THE PRINT AREA
               ORG  92
     X2        DCW  @000@  INDEX FOR THE CIRCULAR BUFFER
               ORG  97
     X3        DCW  @000@  INDEX FOR THE NORMAL FREQUENCY TABLE  
     *
               ORG  100
     *
     * TWO 7-DIGIT SEEDS FOR RANDOM NUMBER GENERATION
     R1        DCW  @1234567@  FIRST SEED
               DC   @XXX@      SPILL AREA FOR RIGHT ROTATE 3
     R2        DCW  @8901234@  SECOND SEED
               DC   @XXXXX@    SPILL AREA FOR RIGHT ROTATE 5
     *
     RRSUM     DCW  @0000000@  SUM OF R1 AND R2
     UNIFRN    EQU  RRSUM-5    GENERATED UNIFORM RANDOM NUMBER
     URNSUM    DCW  @0000@     RUNNING SUM OF LAST 100 UNIFORM RNS
     NORMRN    EQU  URNSUM-2   GENERATED NORMAL RANDOM NUMBER
     OLDEST    DCW  @00@       OLDEST OF 100 LAST UNIFRNS
     NORMCT    DCW  @00000@    COUNT OF NORMAL RANDOM NUMBERS GENERATED
     LINECT    DCW  @000@      COUNT OF LINES PRINTED
     STARCT    DCW  @000@      COUNT OF STARS PRINTED IN A FREQ BAR
     *
               ORG  350
     START     EQU  *&1
     *
     * ZERO OUT THE CIRCULAR BUFFER
     NXTBUF    MCW  @00@,BUFFER&X2
               SBR  X2,2&X2
               C    @200@,X2
               BU   NXTBUF
     *
     * ZERO OUT THE NORMAL FREQUENCY TABLE
     NXTFRQ    MCW  @0000@,NRNFRQ&X3
               SBR  X3,4&X3
               C    @400@,X3
               BU   NXTFRQ
     *
               MCW  @000@,X2
               MCW  @000@,X3
     *
     *----------------------------------------------------
     * TABLE OF 10,000 NORMALLY DISTRIBUTED RANDOM NUMBERS
     *----------------------------------------------------
     *
               MCW  @2-DIGIT RANDOM NUMBERS WITH MEAN 49@,302
               MCW  @TABLE OF 10,000 NORMALLY DISTRIBUTED @
               W
               CS   332
               CS
               W
     *
     * LOOP TO GENERATE THE NEXT RANDOM NUMBER
     *
     NEXTRN    MCW  R1,R1&3       RIGHT ROTATE R1 IN PLACE BY 3
               SW   R1&1
               MCW  R1&3,R1-4
               CW   R1&1
     * 
               MCW  R2,R2&5       RIGHT ROTATE R2 IN PLACE BY 5
               SW   R2&1
               MCW  R2&5,R2-2
               CW   R2&1
     *
               MCW  R2,RRSUM      ADD R1 AND R2
               A    R1,RRSUM
               MZ   @0@,RRSUM-6   CLEAR OVERFLOW
     *
               MCW  R2,R1         R1 = R2
               MCW  RRSUM,R2      R2 = RRSUM
               A    @1@,NORMCT
     *
     * UPDATE THE RUNNING SUM
               MCW  BUFFER&X2,OLDEST
               A    UNIFRN,URNSUM   ADD THE NEWEST UNIFRN
               S    OLDEST,URNSUM   SUBTRACT THE OLDEST UNIFRN
               MZ   @0@,URNSUM      CLEAR ZONE
     *
     * INSERT THE NEWEST UNIFORM RN INTO THE CIRCULAR BUFFER
               MCW  UNIFRN,BUFFER&X2   INSERT
               SBR  X2,2&X2       
               C    @200@,X2           AT THE END OF THE BUFFER?
               BL   UPDATE             NO, GO UPDATE THE FREQUENCY TABLE
               MCW  @000@,X2           YES, WRAP X2 BACK TO THE START
     *
     UPDATE    C    @00100@,NORMCT   AT LEAST 100 UNIFRNS GENERATED?
               BL   NEXTRN           NOT YET  
     *
               MCW  NORMRN,203&X1    MOVE RANDOM NUMBER TO PRINT AREA
     *
     * UPDATE THE NORMAL FREQUENCY TABLE
               MCW  @000@,X3
               MCW  NORMRN,X3        X3 = 4*NORMRN
               A    NORMRN,X3
               A    NORMRN,X3
               A    NORMRN,X3
               A    @1@,NRNFRQ&X3    INCREMENT FREQUENCY ENTRY
     *
     * UPDATE X1 TO PRINT THE NEXT PRINT POSITION
     NEXTPR    SBR  X1,3&X1
               C    @064@,X1      MIDDLE OF THE LINE?
               BU   CHKEOL
               SBR  X1,4&X1       ADD A GAP IN THE MIDDLE
     CHKEOL    C    @128@,X1      END OF THE LINT?
               BU   NEXTRN
     *
     * WRITE ONE LINE
               W
               CS   332
               CS
     *
     * PREPARE FOR THE NEXT LINE
               MCW  @004@,X1
               A    @1@,LINECT
               C    @250@,LINECT    250 LINES PRINTED?
               BU   NEXTRN
     *
     *---------------------------------------------------------------
     * FREQUENCY BAR CHART OF THE NORMALLY DISTRIBUTED RANDOM NUMBERS
     * PRINT ENTRIES 41 THROUGH 58
     *---------------------------------------------------------------
     *
               W
               MCW  @RANDOM NUMBERS@,295
               MCW  @FREQUENCY CHART OF THE NORMALLY DISTRIBUTED @
               W
               CS   332
               CS
               W
     *
               MCW  @041@,NORMRN  START WITH THE 41ST FREQ TABLE ENTRY
               MCW  @164@,X3      INDEX X3 TO THE 41ST ENTRY
     *
     * LOOP TO PRINT THE BAR FOR THE NEXT FREQUENCY VALUE
     *
     NXTBAR    MCW  @000@,X1
               A    @5@,NRNFRQ&X3         ADD 5 TO FREQUENCY
               MCW  NRNFRQ-1&X3,STARCT    RANDOM NUMNBER /10 TO STARCT
     *
     * TO ALLOW THE LONGEST BARS TO FIT, PROGRESSIVELY SHORTEN THE BARS.
     *
     CHK130    C    STARCT,@130@
               BH   CHK120
               S    @11@,STARCT
               B    STARS
     *
     CHK120    C    STARCT,@120@
               BH   CHK110
               S    @10@,STARCT
               B    STARS
     *
     CHK110    C    STARCT,@110@
               BH   CHK100
               S    @9@,STARCT
               B    STARS
     *
     CHK100    C    STARCT,@100@
               BH   CHK90
               S    @8@,STARCT
               B    STARS
     *
     CHK90     C    STARCT,@90@
               BH   CHK80
               S    @7@,STARCT
               B    STARS
     *
     CHK80     C    STARCT,@80@
               BH   CHK70
               S    @6@,STARCT
               B    STARS
     *
     CHK70     C    STARCT,@70@
               BH   CHK60
               S    @5@,STARCT
               B    STARS
     *
     CHK60     C    STARCT,@60@
               BH   CHK50
               S    @4@,STARCT
               B    STARS
     *
     CHK50     C    STARCT,@50@
               BH   CHK40
               S    @3@,STARCT
               B    STARS
     *
     CHK40     C    STARCT,@40@
               BH   CHK30
               S    @2@,STARCT
               B    STARS
     *
     CHK30     C    STARCT,@30@
               BH   STARS
               S    @1@,STARCT
     *
     * LOOP TO PRINT THE STARS IN A BAR
     *
     STARS     MZ   @0@,STARCT    CLEAR ZONE
     NXSTAR    C    STARCT,X1     ENOUGH STARS?
               BE   WRBAR         YES
     *
               MCW  @*@,204&X1    * TO THE PRINT AREA
               SBR  X1,1&X1       X1 TO THE NEXT PRINT POSITION
               B    NXSTAR
     *
     * WRITE A BAR AS THREE LINES OF STARS 
     WRBAR     W
               MCW  NORMRN,202    RANDOM NUMBER TO PRINT AREA
               W
               CS   202
               W
               CS   332
               CS
     *
     * PREPARE FOR THE NEXT LINE
               A    @001@,NORMRN
               SBR  X3,4&X3
               C    @236@,X3      ALREADY PRINTED 58TH ENTRY?
               BL   NXTBAR        NOT YET
     *
               H
     *
     *-------------------------------------------------------
     * CIRCULAR BUFFER OF THE LAST 100 UNIFORM RANDOM NUMBERS
     *-------------------------------------------------------
               ORG  1100
     BTABLE    DA   100X2,X2
     BUFFER    EQU  BTABLE&1
     *
     *-----------------------------------------------------------
     * FREQUENCY TABLE OF THE NORMALLY DISTRIBUTED RANDOM NUMBERS
     *-----------------------------------------------------------
     NRNTAB    DA   100X4,X3
     NRNFRQ    EQU  NRNTAB&3
     *
               END  START
