28th of March, Vectrex Random

I just remembered a thing about “Random” which I encountered a short time ago, which might be worth to mention.

When playing around with Shongololo, one of the tasks was to draw random Shrooms on the screen. For that I wanted to generate random coordinates for a 16×16 grid. One further constraint was – no Shrooms were allowed to occupy the same location.

Using the BIOS function “Random” led to a black screen – and nothing happened.

What happened?

The Random function is not random enough. I wanted to draw more than 32 Shrooms! The Random was only able to generate 32 different pairs of random coordinates! Trying for another set of coordinates led to an endless loop.

Following code was used to generate the coordinates:


Resulting in following Shroom image:

32 two different Shroom locations max!

Not very random looking :-(.

Than I remembered having read about different approaches – and remembered something I wrote in a “C” article of mine…

Ultra Fast Pseudorandom number generator for 8-bit

That translated to assembler resulted in following

looks good now!

Following is the code for possible usage, 4 variables used, one “init” function and a macro. But you might also use a subroutine – which is really straight forward.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
;***************************************************************************
; see https://www.electro-tech-online.com/threads/ultra-fast-pseudorandom-number-generator-for-8-bit.124249/                     BSS      
                    ORG      $c880                        ; start of our ram space 
random_a            ds       1                            ; vars for own "random" - is much better than the internal BIOS one! 
random_b            ds       1                            
random_c            ds       1 
random_x            ds       1 
;***************************************************************************
RANDOM_A_alt        macro    
                    inc      random_x 
                    lda      random_a 
                    eora     random_b 
                    eora     random_x 
                    sta      random_a 
                    adda     random_b 
                    sta      random_b 
                    lsra     
                    eora     random_a 
                    adda     random_c 
                    sta      random_c 
                    endm     
;***************************************************************************
                    code     
                    ORG      0 
initRandom
; initialize random function
; the timer parts should be set from the title screen after the player
; presses a button to play
; 
; will be pretty random than
;
; or just set it here with fixed values
; if you always want the same random seed.
; (vide != vectrex for the timer values)
;
; each value of the 4 "random" variable change causes 
; quite different random outcomes
; so if you just want to test out different "shrooms" on screen
; keep changing e.g. "x"
                    lda      #2
                    sta      random_x 
                    lda      #$d4 
                    sta      random_a 
                    lda      <VIA_t2_lo 
                    sta      random_b 
                    lda      <VIA_t1_cnt_lo 
                    sta      random_c 
                    RANDOM_A_alt                          ; call random once 
				  rts

Tagged on: ,

2 thoughts on “28th of March, Vectrex Random

    1. Malban Post author

      Nope – than you would not need a random function :-).

      But using the low part of a ever changing timer – after a “user-button” press – as a seed seems fairly random to me…

Leave a Reply to Malban Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.