Another Random number generator.

The place for codemasters or beginners to talk about programming any language for the Spectrum.
Post Reply
Nomad
Manic Miner
Posts: 600
Joined: Thu Dec 28, 2017 12:38 pm

Another Random number generator.

Post by Nomad »

I got to thinking last night, with all the headaches of trying to get well distributed random numbers actually generated on the spectrum.. why not cut out the obvious problem (the spectrum as generator).

There was a very popular book issued by RAND that had hundreds of pages of good pseudo random numbers that you could use.

Seems a lot more simple just to use the book to populate a lookup table. This method you take a hit with memory (having the table in memory grows the larger you want the table to be..) But it is a whole lot simpler to implement.

https://www.rand.org/pubs/monograph_reports/MR1418.html

For testing is a nice thing to have because you can just open the book at a page and point your finger and you have your randomly selected pseudo random value. :lol: Not sure about you fellows but a lot of the time I only need a few values to test stuff with.

You might get values that are to large for 16 bits, in that case just select another or subtract 65,535 from the result.

If you were really slick (or not worried about T states you could do 32 bit numbers) :lol:

"You know the z80 wants to be doing 32bit numbers, just look at all the register pairs!"

HL & DE mmmm... all pointing to a 16 bit value in memory..

While there was obviously some prior art, Andre Adrian's website was where I saw this...

Code: Select all

; LOAD TEST VALUES
        LD      HL,01213H       ; LOAD HEXADECIMAL 1213 TO HL (LOWER 16-BIT)
        LD      DE,0F000H       ; LOAD HEXADECIMAL F000 TO DE (LOWER 16-BIT)
        EXX
        LD      HL,01011H       ; LOAD HEXADECIMAL 1011 TO H'L' (UPPER 16-BIT)
        LD      DE,0            ; LOAD VALUE 0 TO D'E' (UPPER 16-BIT)
        EXX
        CALL    ADD32
        HALT

;==================================================
; ADD ROUTINE 32+32BIT=32BIT
; H'L'HL = H'L'HL + D'E'DE
; CHANGES FLAGS
;
ADD32:
        ADD     HL,DE   ; 16-BIT ADD OF HL AND DE
        EXX
        ADC     HL,DE   ; 16-BIT ADD OF HL AND DE WITH CARRY
        EXX

; RESULT IS IN H'L'HL
        RET

        END
 
The EXX op-code was ugly to write and read, but was the reason why everybody doing number crunching in 8-bit liked the Z80 over the 8085.
This website is an great..

http://www.andreadrian.de/oldcpu/Z80_nu ... ocId784223
User avatar
Kweepa
Manic Miner
Posts: 311
Joined: Sat Feb 03, 2018 6:14 pm
Location: Albuquerque, New Mexico

Re: Another Random number generator.

Post by Kweepa »

Yup, can't beat the speed of a lookup table :)
https://github.com/id-Software/DOOM/blo ... m_random.c
User avatar
MonkZy
Manic Miner
Posts: 279
Joined: Thu Feb 08, 2018 1:01 pm

Re: Another Random number generator.

Post by MonkZy »

RANDOM.ORG can produce custom look up tables to paste into code. Tell it the range and how many data sets you want and the size of each set, choose comma separated as output. They use atmospheric data to produce the noise for the random numbers, apparently nature is the best source of random. I made a list of 32 sets of 8 numbers (0-7), the list seems to produce a very 'pattern free' random. A cost of 256 Bytes, not too bad. I think a 128 or 64 byte list would work fine for what i am writing.
User avatar
Joefish
Rick Dangerous
Posts: 2058
Joined: Tue Nov 14, 2017 10:26 am

Re: Another Random number generator.

Post by Joefish »

There's this great website called Google...
http://chuntey.wordpress.com/2012/06/07 ... enerators/
Post Reply