.NLIST TOC,SYM .TITLE $IRAN .SBTTL ULBLIB 042 - RANDOM NUMBER GENERATOR .IDENT \V01.00\ .PSECT .LIBC. .ENABL LC,GBL ; Copyright (c) 1998 by Mentec, Inc., Nashua, NH. ; All rights reserved ; ; This software is furnished under a license for use only on a ; single computer system and may be copied only with the ; inclusion of the above copyright notice. This software, or ; any other copies thereof, may not be provided or otherwise ; made available to any other person except for use on such ; system and to one who agrees to these license terms. Title ; to and ownership of the software shall at all times remain ; in Mentec, Inc. ; ; The information in this document is subject to change without ; notice and should not be construed as a commitment by Digital ; Equipment Corporation, or Mentec, Inc. ; ; Digital and Mentec assume no responsibility for the use or ; reliability of its software on equipment which is not supplied ; by Digital or Mentec, and listed in the Software Product ; Description. ;+ ; $IRAN ; This routine generates a random numbers (hopefully) uniformly distributed ; between 0 and 65535. ; ; To restart the sequence, clear locations RAN.I and RAN.J. Do not modify ; these locations in any other way! ; ; CALL $IRAN ; ; R0 = random number ;- .PSECT .LIBI. RAN.I:: .WORD 0 ;Seed value. High order RAN.J:: .WORD 0 ;Seed value. Low order MULTI: .FLT2 65535. ;Multiplier for range modification .PSECT .LIBC. $IRAN:: JSR R5,$SAVR1 ;Save R1 - R5 MOV #RAN.I,R2 ;R2 -> hi-order seed MOV #RAN.J,R3 ;R3 -> lo-order seed MOV @R2,R0 ;R0 = hi-order seed MOV @R3,R1 ;R1 = lo-order seed BNE 1$ ;Branch if not initial call MOV #3,R1 INC R0 ;Set up 2**16 +3 BR 2$ 1$: ASL R1 ;Multiply by 2 ROL R0 ADD @R2,R0 ;Now by 3 ADD @R3,R1 ADC R0 ADD @R3,R0 ;Now by 2**16 +3 BPL 2$ ;Branch if positive ADD #100000,R0 ;Get 2**32 + G 2$: MOV R0,@R2 ;Store next generator MOV R1,@R3 MOV #201,R2 ;Get initial exponent 3$: ASL R1 ;Float the result ROL R0 BCS 4$ ;Branch if leading bit was found DEC R2 ;Compensate exponent BR 3$ 4$: CLRB R1 BISB R0,R1 SWAB R1 CLRB R0 BISB R2,R0 ;Insert exponent in result SWAB R0 ROR R0 ROR R1 ;Insert + sign MOV #MULTI,R3 ;R3 -> multiplier MOV (R3)+,R2 ;R2 = hi-order multiplier MOV @R3,R3 ;R3 = lo-order multiplier CALL $FMUL ;Multiply them. MOV R2,R1 ;R1 = hi-order result MOV R3,R0 ;R0 = lo-order result ; ; Convert REAL*4 to INTEGER*4 (and throw away the hi-order word ; This is a munged version of the SYSLIB routine JAFIX. ; ASL R1 ;C=SIGN ROR -(SP) ;SAVE FOR LATER SEC RORB R1 ;INSERT IMPLIED NORMALIZE BIT SWAB R1 ;SWITCH EXP AND HIGH MANTISSA MOV R1,R4 ;MAKE COPY FOR EXP CLRB R1 ;REMOVE EXP FROM HIGH WORD MANTISSA BIC R1,R4 ;REMOVE HIGH WORD MANTISSA FROM EXP SWAB R0 ;SET UP TO MOVE NEXT BYTE OF MANTISSA BISB R0,R1 ;INSERT NEXT BYTE MANTISSA CLRB R0 ;REMOVE IT CLR R3 ;INIT LOW WORD OF RESULT CLR R2 ;INIT HIGH WORD OF RESULT SUB #200,R4 ;REMOVE BIAS FROM EXP BLE 7$ ;BRANCH IF RESULT WILL BE ZERO CMP #20,R4 ;WILL RESULT BE ONLY 1 WORD LONG BHIS 6$ ;BRANCH IF ONLY NEED 1 WORD SUB #20,R4 ;WELL, WE NEED 2 WORDS CMP #20,R4 ;MAKE SURE RESULT WILL FIT IN 2 WORDS BHI 5$ ;BRANCH IF O.K. MOV #-2,R0 ;SET OVERFLOW INDICATORE BR 7$ ;GO FINISH UP 5$: MOV R1,R3 ;PERFORM A WORD SHIFT MOV R0,R1 6$: ROL R1 ;SHIFT IN A BIT ROL R3 ;INTO LOW ORDER RESULT ROL R2 ;NOW INTO HIGH ORDER RESULT DEC R4 ;ARE WE DONE? BGT 6$ ;BRANCH FOR MORE BITS 7$: TST (SP)+ ;TEST FINAL SIGN BPL 8$ ;BRANCH IF POSITIVE NEG R3 8$: MOV R3,R0 ;STORE LOW RESULT RETURN .END