Random numbers generation
Posted: Thu Oct 18, 2007 1:04 am
What source can be used as initial state for pseudo-random generator on SMD (to make generator produce different sequences at each power-up)?
Sega Megadrive/Genesis development
https://gendev.spritesmind.net/forum/
Thanks for reminder, I knew this method, but completely forgot about it.ob1 wrote:wait for an input ?
Do you read HBL before setting up video mode? I.e., is that counter resets after mode change or not?TmEE co.(TM) wrote:I've used mixture of default seed(usually my phone number) and HBL counter... always different results.
Can't say it best because it has two flaws. Small one: it needs human to work (unacceptable if random needs before any input, needed rarely). Bigger one: if human hold key(s) from power-up, he be able to always get same random sequences. It good method for most cases though.Stef wrote:But still the best "randomiser" is human, waiting for an input (as ob1 said) is a good solution then ;)
unless you check it on keyup not keydownBigger one: if human hold key(s) from power-up, he be able to always get same random sequences. It good method for most cases though.
Some radioactive mutants may release key on exact frame then;) OK, that better idea.KanedaFr wrote:unless you check it on keyup not keydown ;)
hehe!
How about random algorithms, which one you found most suitable? I though about these two:KanedaFr wrote:I fought a long time to find "the best randomizer for Genny" and finally found the human factor's the best...mainly because I wanted my works to be playable on emulators also
Other than not re-initializing the seed after reset, there really isn't any way. The circuitry is the same every time, with esentially no non-determinstic parts. Even the RAM can be counted on to start up mostly the same on power-up, though I suppose a CRC or checksum of RAM at power-on could get you a little randomness.Shiru wrote:What source can be used as initial state for pseudo-random generator on SMD (to make generator produce different sequences at each power-up)?
Code: Select all
RndSeed DS 4 ; random number seed
. . .
BSR Randomize ; initialize random number generator
. . .
;-----------------------------------------------------------------------
; _Random from Mac Plus, returns a 16-bit random number
;
; EXIT: D0.W = random number 0..65535
;
; * the use of the constant 16807 means this is a
; linear congruential generator (seed * 16807 mod (2^31 - 1))
; * note that the seed must be in the range [1 .. 2^31-1] !!!
; * also note that 2^31-1 is a Mersenne prime
; * for more information, google for: 16807 random
; * this page has a lot of info: http://www.firstpr.com.au/dsp/rand31/
;-----------------------------------------------------------------------
Random
MOVEM.L D1/D2,-(A7)
; this multiplies the 32-bit seed by 16807
MOVE.W #16807,D0 ; D0,D2 = multiplication constant
MOVE.W D0,D2
MULU RndSeed+2,D0 ; multiply low word
MOVE.L D0,D1 ; D1 = high word of product
CLR.W D1
SWAP D1
MULU RndSeed,D2 ; multiply high word
ADD.L D1,D2 ; add high of first multiply to result of second multiply
; this does the mod (2^31-1)
MOVE.L D2,D1 ; save in both D1 and D2
ADD.L D1,D1 ; multiply high word of result by 2
CLR.W D1
SWAP D1
ANDI.L #$0000FFFF,D0
SUBI.L #$7FFFFFFF,D0 ; =2^31-1
ANDI.L #$00007FFF,D2
SWAP D2
ADD.L D1,D2
ADD.L D2,D0
BPL.B .10 ; add 2^31-1 once more if bit 31 is set
ADDI.L #$7FFFFFFF,D0 ; =2^31-1
.10
MOVE.L D0,RndSeed ; save result as new random number seed
MOVEM.L (A7)+,D1/D2
RTS
;-----------------------------------------------------------------------
; Generate a new random number seed using the current frame counter
;-----------------------------------------------------------------------
Randomize
MOVE.L RndSeed,D0 ; start with previous random seed
MOVE.L VTimer,D1 ; XOR with current frame counter
EOR D1,D0
BCLR #31,D0 ; clear high bit to ensure <= 2^31-1
BNE.S .10
ADDQ.L #1,D0 ; don't allow a seed of zero
.10
MOVE.L D0,RndSeed ; save new seed
ADDQ.L #1,D0 ; see if it was == 2^31-1
BPL.S .20
SUBQ.L #1,RndSeed ; don't allow a seed of 2^31-1
.20
RTS
There is ways, TmEE and I already mentioned two of them (HBL counter, register R of Z80).8bitwizard wrote:Other than not re-initializing the seed after reset, there really isn't any way.
States of logic elements like triggers on power-up usually is non-determined.8bitwizard wrote:The circuitry is the same every time, with esentially no non-determinstic parts.
It must be different, although garbage pattern always similar (but not exact). I'm not take that way because counting of CRC is long and more complex than other possible ways.8bitwizard wrote:Even the RAM can be counted on to start up mostly the same on power-up
Why I must call random generator every VSync? I need only one random value at start of whole program to use it as initial state of RNG. Generator then called each time when I need random number, and that can be many times per frame.8bitwizard wrote:The simplest way is to simply call your random number generator on every Vsync interrupt