[comp.os.msdos.programmer] Random number generation

djb@bbt.UUCP (beauvais) (09/07/90)

In article <20092@orstcs.CS.ORST.EDU> densond@prism.CS.ORST.EDU (Dave Denson) writes:
>I need a way to generate suitably random numbers in an assembly language 
>program.


If it's non-critical, just try reading the time of day.  It is updated 18.2
times per second.  This is fine for a game, but surely isn't a true
randomly distributed set.


-- 
Dan Beauvais                        UUCP:      ...!mcnc!rti!bbt!djb
BroadBand Technologies, Inc.        Internet:  djb%bbt@rti.rti.org
Box 13737                           BITNET:    djb%bbt%rti.rti.org@CUNYVM
Research Triangle Park, NC  27709   +1 (919)-544-6850 ext. 295

ee5391aa@hydra.unm.edu (Duke McMullan n5gax) (09/15/90)

In article <20092@orstcs.CS.ORST.EDU> densond@prism.CS.ORST.EDU (Dave Denson)
writes:
>I need a way to generate suitably random numbers in an assembly language 
>program.

Hmmmmm. If you can handle 16-bit integer arithmetic efficiently .and. you can
use _floating_point_ numbers for the output (range is [0,1), I recall, very
uniform), then look in the March 87 issue of Byte.

The article is by Wichmann and Hill, and gives (I think) a Pascal implemen-
tation. Pages 127-8.

If you wish, email me, and I'll send you the C source I wrote from their
Pascal version. The algorithm is simple, but (according to the authors) very
effective. It's performed well for me, but I've not subjected to any stiff
testing.


						Best o' luck,
							d


--
  "...while I know many people who emphatically believe in reincarnation, I
have never met or read one who could satisfactorily explain population growth."
						-- Spider Robinson
   Duke McMullan n5gax nss13429r phon505-255-4642 ee5391aa@hydra.unm.edu

everett@hpcvra.CV.HP.COM (Everett Kaser) (09/17/90)

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

rush@xanadu.llnl.gov (Alan Edwards) (09/26/90)

In article <20092@orstcs.CS.ORST.EDU> densond@prism.CS.ORST.EDU,
Dave Denson, writes:
>I need a way to generate suitably random numbers in an assembly language 
>program.

If you are serious about it, here is a book to check out:

_Seminumerical Algorithms_ volume 2, by Donald Knuth,
published by Addison Wesley

There is a thorough duscussion of several methods of generating random
numbers which includes discussion of how good or bad they are.

Actually, you should be able to find a comparable book at your local
college library.

Glad to help,
-Alan
-- 
 .------------------------------------.
 | Alan Edwards: rush@xanadu.llnl.gov |      I post...therefore I am.
 |   or: rush%xanadu@lll-crg.llnl.gov |                ;-)
 `------------------------------------'

hughes@volcano.Berkeley.EDU (Eric Hughes) (09/29/90)

In article <20092@orstcs.CS.ORST.EDU> densond@prism.CS.ORST.EDU (Dave
Denson) writes:
>I need a way to generate suitably random numbers in an assembly language 
>program.  Is there a routine somewhere out there that exist to accomplish
>this.  I could create your standard MOD-type generator, but I was hoping
>for a more elegant solution.

Assuming "suitable" in for case does not mean "random," but just
"evenly distributed," check the January 1990 issue of Communications
of the ACM for the article "Two Fast Implementations of the 'Minimal
Standard' Random Number Generator."  This is a mod-type generator.

And if it suffices, then, by virtue of its simplicity, it _is_ an
elegant solution.

Eric Hughes
hughes@ocf.berkeley.edu