NESDev and Strangulation Records messageboards
Forum Index | FAQ | New User | Login | Search

Previous ThreadView All ThreadsNext Thread*Show in Threaded Mode


SubjectRandom numbers  
Posted byJaywalk
Posted on3/1/02 09:33 AM
From IP152.106.98.1  



I just can't figure out a way to make them on a NES. How on earth can you live without random numbers? Can anyone help?




SubjectRe: Random numbers new  
Posted byMemblers
Posted on3/1/02 9:44 PM
From IP206.150.216.119  



Use the controllers or some kind input to get a 'seed' value. Like, during an intro or something you could INC a number every frame while it's waiting for the user to press a button. Then you do weird stuff with that value. Here's what I used in one of my test programs.

random_gen:
ldx #32 ; generate 4 bytes
@loop:
lda random4 ; random bit generator
rol ;
rol ; uses user input from joystick for initial values
rol
rol
eor random4 ; this will XOR two bits together and put the answer in bit 7 of the acc
rol ; this puts the answer into the carry
ror random1
ror random2
ror random3
ror random4
dex
bne @loop
rts




SubjectRe: Random numbers new  
Posted byzero soul
Posted on3/1/02 9:44 PM



what I usually do is have a byte (usually labeled 'TIMER') and increment it in the NMI. I use it as a random-number base (if I need it), although usually as a literal timer. the problem with it is that if you need several numbers in a row (ie., more than one in a particular frame), you need several 'timers'.

see if that helps.


...just another vision...


SubjectRe: Random numbers new  
Posted byTricob
Posted on3/4/02 3:38 PM
From IP216.76.232.226  



Isn't there some sort of random number generator in the "Solar Wars" source code?

- Tricob.


SubjectRe: Random numbers new  
Posted bymcmartin
Posted on3/6/02 02:12 AM
From IP12.234.29.87  



Warning: Probable massive overkill here, but, I'll give a full explanation, then provide some code to do it. Eventually I should put this up more fully somewhere, I imagine...

My "Galaxy Patrol" game needs to randomly generate its map, so I had to make a full-blown random number generator. One of the simplest forms is a "Linear Congruential Generator" or LCG. This starts with some seed value T, then advances itself by computing

T = (a*T + b) MOD c

a, b, and c generally have to be rather large, and have special properties, so you need a full 32-bit multiplier and a bit of research. Fortunately, http://www.6502.org provides basic code for a 32-bit multiplier, and a little web research popped up a lot of generators. I chose the BCPL generator because c takes the value 2^32, and computing MOD 2^32 is really easy. (As an added bonus, its seed value is zero.)

The code for the BCPL LCG random number generator is as follows.

seed'random:
lda #$00
sta random
sta random+1
sta random+2
sta random+3
rts

update'random:
lda random'mult
sta MULND
lda random'mult+1
sta MULND+1
lda random'mult+2
sta MULND+2
lda random'mult+3
sta MULND+3
lda random
sta MULR
lda random+1
sta MULR+1
lda random+2
sta MULR+2
lda random+3
sta MULR+3
jsr mult32
clc
lda PROD
adc random'add
sta random
lda PROD+1
adc random'add+1
sta random+1
lda PROD+2
adc random'add+2
sta random+2
lda PROD+3
adc random'add+3
sta random+3
rts

random'mult:
.byte $6d, $83, $10, $90

random'add:
.byte $31, $1d, $a0, $2a

; Workspace:
; PROD: 8 bytes. Holds 64-bit result.
; MULND: 4 bytes. 32-bit multiplicand.
; MULR: 4 bytes. 32-bit multiplier.

; 32 bit multiply with 64 bit product

mult32: lda #$00
sta PROD+4 ;Clear upper half of
sta PROD+5 ;product
sta PROD+6
sta PROD+7
ldx #$20 ;Set binary count to 32
* lsr MULR+3 ;Shift multiplyer right
ror MULR+2
ror MULR+1
ror MULR
bcc + ;Go rotate right if c = 0
lda PROD+4 ;Get upper half of product
clc ; and add multiplicand to
adc MULND ; it
sta PROD+4
lda PROD+5
adc MULND+1
sta PROD+5
lda PROD+6
adc MULND+2
sta PROD+6
lda PROD+7
adc MULND+3
* ror ; Rotate partial product
sta PROD+7 ; right
ror PROD+6
ror PROD+5
ror PROD+4
ror PROD+3
ror PROD+2
ror PROD+1
ror PROD
dex ; Decrement bit count and
bne -- ; loop until 32 bits are done
rts


To get random bytes, read from the "random" array. (random, prod, mulnd, and mulr are defined elsewhere in the source because the version of the assembler I had at the time needed RAM variables declared in a special section.)

-- Michael
http://hkn.berkeley.edu/~mcmartin/retro/




SubjectRe: Random numbers new  
Posted byJaywalk
Posted on3/6/02 07:23 AM
From IP152.106.98.1  



Thanks. That is probably an overkill for what I'm doing at the moment, but exactly what I was hoping for. Thanks again.




SubjectRe: Random numbers new  
Posted byccovell
Posted on3/13/02 02:35 AM
From IP61.199.194.97  



Yes, in Solar Wars, I used a simple and bad random number generator to generate the terrain location and to position the players. I just waited for a player's buttonpress while INCing variables.

--
Chris Covell (chris_covell@yahoo.ca)
Solar Wars Homepage!
http://www.zyx.com/chrisc


SubjectRe: Random numbers new  
Posted byAnonymous
Posted on3/17/02 2:02 PM
From IP195.197.160.10  



Make a seed variable, then just use ADC to add all changing register variables (Joypad registers,etc) together. If you use DPCM samples, use IRQ to do this... Otherwise do this at NMI... Use them together, etc, etc... Use this value directly as random number or use it as index to bigger random-number array. Experiment with it!

- Mankeli / HNO3




Previous ThreadView All ThreadsNext Thread*Show in Threaded Mode
Jump to

Memblers' homepage             Contact Me

Forums powered by WWWThreads Demo