Re: A83: Random number


[Prev][Next][Index][Thread]

Re: A83: Random number




In a message dated 29/02/00 16:50:15 GMT Standard Time, marcputs@hetnet.nl 
writes:

<< How can I generate a random number? (0 or 1) >>

Here is the random number generator I used in Lode Runner 83. [R2 now
available :)]

It uses 2 seed values. One is advanced by multiplying it by 4 and
adding 17 ; the second is used as an index into the program itself,
on the grounds that it should give a randomish sequence of values :)
The 2 values are combined towards the end to give a pseudo
random byte value.

The TI83 ROM space could also be used as a source of randomish
values.

To convert to 0 or 1 just AND $01

Paul Robson (autismuk@aol.com)


; ***********************************************************************
;
;                       Random number generator.
;
; ***********************************************************************

Random: push    de
        push    hl

        ld      hl,(Seed2)              ; do some tinkering
        add     hl,hl
        add     hl,hl
        ld      de,17
        add     hl,de
        ld      (Seed2),hl

        ld      hl,(Seed1)              ; bump seed 1 value
        ld      de,1
        add     hl,de
        ld      (Seed1),hl
        ld      a,h                     ; seed 1 is now a value from $0000
        and     $0F                     ; to $0FFF
        ld      h,a
        ld      de,$9327                ; add to base of program
        add     hl,de

        ld      a,(hl)                  ; get value
        ld      hl,(Seed2)              ; modify it with Seed2
        xor     h
        add     a,l

        pop     hl
        pop     de
        ret

Seed1:  .dw     0
Seed2:  .dw     0