Pseudo Random number generator | ASM/8086 |
Here is a small pseudo random generator. I found it on Andrew Griffinis homepage, but I seem to have misslaid his e-mail and homepage address.;
; pseudo random number generator
;
; input:
; bp = seed (!=0)
;
; output:
; ax = random word
;
; destroys:
; bp
; flags
;
mov al,16
r_loop: rol bp,1
jnc r_skip
xor bp,0ah
r_skip: dec al
jne r_loop
mov ax,bp
One improvement that can be done is to replace store BP
someway so you can reuse it later in the random routine.
Max McGuire has submitted a new version (note that in order for this version to run on a 286 and below, FS
has to be replaced with another segment register):
I'm submitting to you an alternative pseudo random generator that I found in the source for the intro Chaos by Consub of CSB. Here's his original code:
mov ax,fs
mov bx,13
mul bx
xchg ah,al
mov fs,ax
I kept the same basic idea, but optimized (at least I think I optimized) this code for speed by making a few changes (I also changed the seed from fs to dx): mov bx,dx
shl dx,2
add dx,bx
xchg dl,dh
I don't know if this algorithm generates random numbers that are uniformly distributed between 0 and 65535.