78. Paul D. DeRocco  	  May 2 1997, 2:00 am     show options
Newsgroups: rec.games.programmer, alt.msdos.programmer, comp.os.msdos.programmer, comp.programming, comp.sys.ibm.pc.demos, rec.games.programmer
From: "Paul D. DeRocco" <strip_these_words_pdero...@ix.netcom.com> - Find messages by this author
Date: 1997/05/02
Subject: Re: Random Number Generator CPP
Reply to Author | Forward | Print | Individual Message | Show original | Report Abuse

Chris Watts wrote:

> I'm just starting to program in C++ and I am having a hard time
> implementing the rand() function (from stdlib.h).  Would someone be able
> to ribble off a function or something where it would pick a random
> integer between 65 and 90 and return it to the main program? Thanks in
> advance.

You can use this function written in inline assembler:

unsigned long randseed;

unsigned int rand() {

    asm {
        mov ax, word ptr randseed[2]
        mov dx, word ptr randseed[0]
        mov bx, ax
        mov cx, dx
        shl cx, 3
        shr bh, 4
        or cl, bh
        xor dx, cx
        not dx
        shl dx, 1
        rcl ax, 1
        shr dx, 1
        mov word ptr randseed[0], ax
        mov word ptr randseed[2], dx
        }
    }

(I'm assuming 16-bit x86 code, given what newsgroup this is.) Note that
this requires at least a 186 CPU (Does anyone care about 8088
compatibility any more?), so you'll have to use the appropriate compiler
option (-1 for Borland). This produces excellent results, repeating
every 2^31-1 calls. 