9. Matthew Kirkwood  	  May 14 1996, 2:00 am     show options
Newsgroups: comp.lang.asm.x86
From: Matthew Kirkwood <weej...@ferret.lmh.ox.ac.uk> - Find messages by this author
Date: 1996/05/14
Subject: Re: random routine
Reply to Author | Forward | Print | Individual Message | Show original | Report Abuse

> I need a random routine!  I've tried to use the timer, but all values had
> the same distance.

> Thanks cynic

Here's some source from one of my assembler libraries.  There are two
random seed/get routines, both called in the same way.

Seed - Input AX = seed, or if (AX == 0) use BIOS data area clock count
Get  - Just call it.  AX = Random number.

They're both quite slow - the first uses a multiply, and the second
messes around with indexing and stuff.

The first goes through all 2^16 = 65536 possible numbers before looping.

The second (KnuthRnd) apparently goes through about 2^55 before looping,
although it clearly reapeats individual numbers before this.

Anyway - Here you are:  (Assemble with A86 by the way)

=================== RAND.8 ===================
; ********** Random Number Routines **********
PUBLIC SeedRand,GetRand

_DATA SEGMENT PARA PUBLIC 'DATA'
LastRand        DW ?
_DATA ENDS

_CODE SEGMENT PARA PUBLIC 'CODE'
SeedRand:                               ; Seeds random number generator with
                                        ; no. in AX (or Clock Count if zero)
    TEST AX
    JNZ >S001
    PUSH DS
    MOV DS,AX,040
    MOV AX,[06C]                        ; Get low timer tick word
    ADD AX,[06E]                        ; Add high word
    POP DS
S001:
    MOV LastRand,AX                     ; Store
    RET

GetRand:                                ; Returns a Pseudo-Random number in AX
    PUSH DX
    MOV AX,LastRand
    MOV DX,9421
    MUL DX
    INC AX
    MOV LastRand,AX
    POP DX
    RET
_CODE ENDS 