Re: Random-number generation
Posted: Sat Jan 28, 2023 8:33 pm
so far my go-to RNG has been a function that takes a 32-bit number, splits it into 4x 8-bit pointers that point to a predefined set of 256 values. a number is read from each pointer, Added together, XOR'd, and Added again, to get a final result. and then increments the 32-bit number by 1.
so it uses 4 bytes of Memory, and 256 bytes for the table+function (because the code of the function is used as part of the table, to save some memory)
since it just uses a 32-bit incrementing number it will repeat after 2^32-1 calls.
so it uses 4 bytes of Memory, and 256 bytes for the table+function (because the code of the function is used as part of the table, to save some memory)
Code: Select all
.data
rng_ptr: .res 4
.code
rng_func:
.A8
.I8
LDX a:rng_ptr
LDY a:rng_ptr + 1 ; Get Pointers 0 and 1
LDA a:rng_func,X ; Get a byte from the RNG Table (using Pointer 0)
CLC
ADC rng_func,Y ; Add a byte from the RNG Table (using Pointer 1) to the Value
LDX rng_ptr + 2
LDY rng_ptr + 3 ; Get Pointers 2 and 3
EOR a:rng_func,X ; XOR the Value with a byte from the RNG Table (using Pointer 2)
ADC rng_func,Y ; And finally, Add a byte from the RNG Table (using Pointer 3) to the Value
PHA ; Save the Output on the Stack
INC a:rng_ptr
BNE @s
INC a:rng_ptr + 1
BNE @s
INC a:rng_ptr + 2
BNE @s
INC a:rng_ptr + 3
@s: PLA ; Get the Output from the Stack again, then Return
RTS
; Function takes up 46 Bytes, so another 210 Bytes are required to fill the table
.byte $54, $67, $87, $EC, $DF, $A0, $0F, $A0, $9E, $F9, $40, $03, $9A, $B7, $EE, $68
.byte $3C, $8E, $79, $B4, $D6, $1D, $E5, $43, $54, $65, $DB, $59, $A2, $A6, $AD, $A9
.byte $75, $50, $BB, $BA, $82, $F4, $D3, $3C, $50, $5E, $6A, $98, $2E, $49, $CB, $3F
.byte $02, $53, $93, $8F, $EA, $24, $1C, $57, $F5, $F0, $AD, $3F, $65, $BA, $94, $A7
.byte $43, $12, $79, $CB, $30, $2F, $93, $2F, $BB, $94, $92, $A8, $BE, $AE, $5F, $88
.byte $66, $11, $0B, $BA, $FE, $48, $8C, $E2, $A4, $64, $21, $BF, $75, $A6, $78, $62
.byte $D8, $0A, $F9, $0F, $FA, $89, $CB, $BF, $A4, $47, $70, $A9, $F5, $1D, $16, $3F
.byte $B3, $20, $F9, $90, $33, $1B, $74, $F6, $16, $25, $90, $79, $4D, $BA, $46, $61
.byte $30, $0B, $B5, $CA, $95, $6E, $FA, $48, $2E, $15, $7F, $E2, $A0, $7F, $DF, $F4
.byte $17, $4D, $BA, $BE, $54, $64, $12, $B7, $64, $8D, $17, $E2, $90, $F9, $70, $BE
.byte $2D, $5B, $6B, $95, $5D, $80, $A1, $37, $E6, $92, $FE, $73, $B3, $71, $2F, $CB
.byte $A6, $D7, $EE, $48, $CA, $1C, $AD, $5E, $09, $E8, $98, $3F, $01, $1B, $EC, $25
.byte $95, $B5, $1D, $5B, $4E, $93, $4A, $11, $B9, $44, $F2, $4C, $44, $46, $FE, $7D
.byte $58, $73