Path: utzoo!attcan!uunet!know!zaphod.mps.ohio-state.edu!sdd.hp.com!hp-pcd!hpcvra!everett From: everett@hpcvra.CV.HP.COM (Everett Kaser) Newsgroups: comp.os.msdos.programmer Subject: Re: Random number generation Message-ID: <32430003@hpcvra.CV.HP.COM> Date: 17 Sep 90 16:13:15 GMT References: <20092@orstcs.CS.ORST.EDU> Organization: Hewlett-Packard Co., Corvallis, OR, USA Lines: 55 Here's some code that I've used in the past (but not what I use currently). This code is based upon a very common, popular formula, whose success depends very much upon the two constants chosen. These are ones that I came up with, but they don't work as well as the Microsoft C rand function (which operates with the same algorithm, different constants and number sizes. However, I can't publish Microsofts rand code, being a reasonably moral person, so you'd have to disassemble their library function yourself :-). --------------------------------------- cut here ---------------------- ; THIS CODE ASSUMES THAT CS and DS are equal. seed dw 4 ; ; setrand - set random # seed ; setrand: or ax,ax ; if seed specified jnz setit ; use it mov ah,2ch ; else int 21h ; use system time (secs & mov ax,dx ; 1/100's) to get rand seed setit: mov seed,ax ; save seed ret ; that's all folks ; ; rand - fetch a new random number; at entry, cx=limit ; at exit, ax=random number ; ; formula: rand(i+1)=261*rand(i) mod 65521 ; value returned: rand(i+1) mod CX ; obviously, based on the two formulas above, the max rand number is 65520. ; rand: mov ax,seed ; get last rand mov bx,261 ; multiplier mul bx ; mov bx,65521 div bx cmp dx,0 ; new seed=0? jnz seedok ; jif no mov ah,2ch ; else int 21h ; use system time (secs & mov seed,dx ; 1/100's) to get rand seed jmp rand ; try again seedok: mov seed,dx mov ax,dx mov dx,0 div cx ; get new_rand mod CX (limit value) mov ax,dx ret ; that's all ; -------------------------------- that's it ------------------------- Everett Kaser Hewlett-Packard Company ...hplabs!hp-pcd!everett work: (503) 750-3569 Corvallis, Oregon everett%hpcvra@hplabs.hp.com home: (503) 928-5259 Albany, Oregon